Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JIT #72

Open
davidbrochart opened this issue Mar 9, 2021 · 3 comments
Open

JIT #72

davidbrochart opened this issue Mar 9, 2021 · 3 comments

Comments

@davidbrochart
Copy link

Would it make sense to allow for just-in-time generation (and execution) of the synchronous code?

@sethmlarson
Copy link
Contributor

You'd run into problems where the code is used within a read-only file system. There would also be problems with type hints/suggestions when developing with the code.

@pquentin
Copy link
Member

Right, JIT seems like too much magic. Why would you need that?

Reading jupyter/jupyter_client#624, what seems more useful is to be able to tell unasync to ignore code that is deliberately async. Another option is to call async code from the sync parts, possibly using the world's simplest coroutine runner:

def _run_secretly_sync_async_fn(async_fn, *args, **kwargs):
    coro = async_fn(*args, **kwargs)
    try:
        coro.send(None)
    except StopIteration as exc:
        return exc.value
    else:
        raise RuntimeError("this async function is not secretly synchronous")

(Even though that won't help for _async_poll_for_reply in nbclient.)

For what it's worth, unasync is currently used successfully in httpcore and the Python Elasticsearch client. As as unasync maintainer, I'm very interested to see use cases where it does not work, so I subscribed to that issue and will be interested to see what comes out of it.

@davidbrochart
Copy link
Author

Right, JIT seems like too much magic. Why would you need that?

Essentially to not worry about the generated sync files: whether to commit them or not, having the extra step of generating them each time the async code changes... Generating sync code on the fly would feel much more like there is one source of truth.

Reading jupyter/jupyter_client#624, what seems more useful is to be able to tell unasync to ignore code that is deliberately async.

But then in the sync world you'll have to run this code until complete in an event loop, which is what we wanted to avoid with unasync.

Another option is to call async code from the sync parts, possibly using the world's simplest coroutine runner:

def _run_secretly_sync_async_fn(async_fn, *args, **kwargs):
    coro = async_fn(*args, **kwargs)
    try:
        coro.send(None)
    except StopIteration as exc:
        return exc.value
    else:
        raise RuntimeError("this async function is not secretly synchronous")

(Even though that won't help for _async_poll_for_reply in nbclient.)

I don't see how you can run async code with this function, but yes running a coroutine until complete in an event loop is an option, although it also comes with complications when an event loop is already running.

For what it's worth, unasync is currently used successfully in httpcore and the Python Elasticsearch client. As as unasync maintainer, I'm very interested to see use cases where it does not work, so I subscribed to that issue and will be interested to see what comes out of it.

Good to know, for jupyter-client I came to the conclusion that unasync is not an option, as we (can) have async code that is impossible to translate to sync.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants