question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Just 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 run input until complete in the current event loop (nest-asyncio Can't patch loop of type <class 'pyodide.webloop.WebLoop'>).

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:3
  • Comments:13 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
joemarshallcommented, May 19, 2022

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.

0reactions
alexmojakicommented, Oct 17, 2022

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.

In my ideal magical universe there’d be a single piece of code that allowed synchronous communication between kernel and front-end, which would either use service workers, or web threading primitives if they were available because they’re nicer but require server headers.

https://github.com/alexmojaki/sync-message aims to provide this.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found