await input()
See original GitHub issueJust wanted to share this issue in case someone has ideas.
Problem
With pyolite we can now get a user input with:
i = await input()
which is different than what we are used to in ipython:
i = input()
The fact that we need to await
is problematic because it means that wherever this is used, the whole call stack must now be async (you cannot use it in a regular Python function).
Proposed Solution
There is already a running event loop in Jupyterlite, so we can’t just run input
until complete.
There are two tricks that I know of to work around that, unfortunately they don’t work:
- running
input
until complete in another thread with its own event loop (pyolite doesn’t support threads). - using
nest-asyncio
and runinput
until complete in the current event loop (nest-asyncioCan't patch loop of type <class 'pyodide.webloop.WebLoop'>
).
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:13 (10 by maintainers)
Top Results From Across the Web
How to use JavaScript await on user input in an async function
Create a promise, resolve it inside popup closed event handler, and await for it inside your function.
Read more >javascript - await user input with async/await syntax
I would love to not have to only use async/await to wait for web resources to come back but also to wait for...
Read more >Tutorial: Asynchronous Input - CodeHS
Asynchronous functions are different than our blocking, synchronous functions. These functions allow the browser to continue executing while they wait to ...
Read more >aconsole - PyPI
Provides async await for print and input. ... See: Multiple inputs awaiting. ... asyncio.get_event_loop() console = AsyncConsole() console.title('echo ...
Read more >Async Generators for User Input - Whistlr
This is thanks to async generators and the await keyword—are you sick of a ... 1) { resolve(); // allow the generator to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It is possible to do this if you use a service worker. I haven’t done it in my jupyter because I didn’t need it, but I do use a service worker for other things that need to be async in javascript whilst sync in the python webworker.
How it works is the service worker handles fetch requests, which you can trigger from a webworker (like pyolite is) by calling synchronous xmlhttprequest for a special URL that you define. The XMLHttpRequest waits until the fetch responds. So you can call the request, then in the serviceworker fire off whatever async things you want, and only respond to the fetch once that is done.
This can also be done to implement time.sleep without having to use anything clever which is nice.
Just came across someone confused by this now: https://www.reddit.com/r/learnpython/comments/y6jzzd/input_coming_back_as_a_future/
I got here by Googling “jupyterlite input future” but saw several other search results from e.g. StackOverflow from confused people.
https://github.com/alexmojaki/sync-message aims to provide this.