Last active
December 5, 2017 01:28
-
-
Save freakboy3742/c6a67d6594f6e9c741ff4151a34ccc51 to your computer and use it in GitHub Desktop.
Asyncio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
async def stuff(val): | |
print("Going to sleep for", val) | |
await asyncio.sleep(val) | |
print("slept") | |
return val * 2 | |
def do_stuff(): | |
print("do stuff") | |
task = asyncio.ensure_future(stuff(1)) | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(task) | |
print("Task result is", task.result()) | |
async def main(): | |
print("Start main") | |
do_stuff() | |
print("Main complete") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> do_stuff() | |
do stuff | |
Going to sleep for 1 | |
slept | |
Task result is 2 | |
>>> loop = asyncio.get_event_loop() | |
>>> loop.run_until_complete(main()) | |
Traceback | |
... | |
RuntimeError: This event loop is already running |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@dpnova But I think that
WKWebView
must be registered with the event loop on the main(UI) thread.Also, even if this task delegated to another thread, using any synchronous way to get the result will still block the main thread and freeze the GUI.
The intention of the original issue(beeware/toga#68) is that I want to effectively avoid cases like this to freeze the GUI.