Async and Concurrency (for dummies)

Hey,

For those of you who use and love Panel but are no professionals like myself here’s a little help for Async and Concurrency. It’s a “bit more” straightforward than the official user guide’s description.

Please, see code snippet and link below for details. Tried and tested on Heroku.

@unsync
async def unsync_async():
   await asyncio.sleep(0.1)
   return 'I like decorators'

unsync_async().result()

Unsync

To give you an idea…
Declare a widget with placeholder info for initial load then in the background run and async function to update its value/object/etc.

Hi,
That looks interesting.
If I understand this correctly, everything still happens in the same thread right? So there is no need for pn.io.unlocked() ? Does this keep the GUI responsive while the task is being executed?

Also, perhaps a long shot, any way of making this work with dask?
For what I’d like to do see this post here: Panel webapp with dask/bokeh/tornado and asynchronous tasks - #5 by Jhsmit

Is this equivalent to pn.state.onload(load_data) Defer data load — Panel 0.11.3 documentation

@Jhsmit @ahuang11
What I can say is that the GUI does stay responsive. I can’t confirm your other questions regarding threads/Dask/etc.

An example I can give you. A Panel app running on Heroku took about 30 seconds to load up and this was not due to dyno sleep/boot time. During the 30 seconds data got downloaded and processed from database to be turned into lists for select widgets, datepicker widgets, etc.

Once I started using the above method, the app load time dropped below 5 seconds and was responsive before the widgets’ objects/values got updated, if that makes sense.

For me the main advantage of this method is its readability. Apart from speed of course…

Also, for those of you not familiar with unsync… when you are calling your function, you don’t have to use .result()

Further to this, check out Unsync’s GitHub page to have a better understanding of how you can use the @unsync decorator with normal functions or with async functions, how you can mix them, how you can chain them. How .result() works. What these mean in terms of threading, etc.

Unsync - GitHub

…and all the above seems to be working with Panel.

I’ll have a look, my last tangle with Dask i had issues because the dask future’s are different from asyncio features. But perhaps this library can help.

What I’m using now with the period callback seems to incur high CPU usage on client PC’s…