denosaurs / deno_python

🐍 Python interpreter bindings for Deno and Bun.
https://deno.land/x/python
MIT License
679 stars 21 forks source link

Synchronous Python & async JavaScript? #36

Open cshaa opened 1 year ago

cshaa commented 1 year ago

In contrast to most JavaScript code, Python libraries often work synchronously and may even expect one's callback function to resolve synchronously, even if that doesn't make sense in JavaScript environment. One example might be the audible Python package that performs all HTTP requests synchronously and expects a synchronous callback to show an image to the user and get their response.

I have no experience with Deno's FFI yet, but it seems to me that calling synchronous Python functions asynchronously should be possible, as the Python interpreter runs in a different thread. Similarly, I would expect "converting" asynchronous JS functions to synchronous Python callbacks should be possible, if one blocks the Python interpreter until the JS promise is settled.

Is this something that sounds viable to you? And if so, is this something you'd be interested in adding to this library?

DjDeveloperr commented 1 year ago

While it should be possible to call sync Python functions asynchronously, the latter doesn't seem to work out. I tried it out and there is a deadlock. JS event loop does not step in an FFI callback's context which is further being waited upon by Python.