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

Asyncio Python API - Request For Comment #3

Open
heri16 opened this issue May 4, 2024 · 4 comments
Open

Asyncio Python API - Request For Comment #3

heri16 opened this issue May 4, 2024 · 4 comments

Comments

@heri16
Copy link
Contributor

heri16 commented May 4, 2024

Would like to add asyncio support to bun_python.

Before I submit the PR, would like to get some thoughts on which API is preferable:

Option A:

import { python, asyncio } from 'bun_python'

const {
    say_hello,
} = python.runModule(await Bun.file(path.resolve(import.meta.dir, 'example.py')).text(), 'example.py')

python.run_loop() // returns a promise that resolves when the loop stops

await asyncio(say_hello('world'), timeout_ms)

main().finally(() => python.stop_loop())

Option A has an asyncio() function to wrap Py Coroutines or Py asyncio.Task into an awaitable JS Promise.

Option B:

import { python } from 'bun_python'

const {
    say_hello,
} = python.runModule(await Bun.file(path.resolve(import.meta.dir, 'example.py')).text(), 'example.py')

python.run_loop()  // returns a promise that resolves when the loop stops

await say_hello('world')

main().finally(() => python.stop_loop())

Option B adds a then() method to all PyObject proxy to make them awaitable.

@codehz

@heri16 heri16 changed the title Asyncio Python API Asyncio Python API - Request For Comment May 4, 2024
@codehz
Copy link
Owner

codehz commented May 5, 2024

I prefer to choose Option B
But I doubt whether this operation will really work... The event loop should block the entire thread, so run_loop should not be able to use promise...

@heri16
Copy link
Contributor Author

heri16 commented May 5, 2024

I have a version of it that already works. It depends on python Async functions not monopolizing or holding up the event loop, by not doing any long blocking operations on the main thread, which most python async code today already does.

Also any code in python that is blocking more than a few microseconds, should be wrapped inside asyncio.to_thread(blocking_func). Other wise there will be microstuttering. Also, the GIL lock needs to be released so the thread can use the GIL.

@heri16
Copy link
Contributor Author

heri16 commented May 5, 2024

For Option B,

The PyCoro_Check() function in the Python C-API can quickly determine if a given PyObject is a coroutine. The performance characteristics of this function is generally very good because it's implemented as a simple type check.

Shall I go ahead and make a PR based on this? @codehz

@codehz
Copy link
Owner

codehz commented May 5, 2024

seems ok @heri16

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

2 participants