oremanj / greenback

Reenter an asyncio or Trio event loop from synchronous code
https://greenback.readthedocs.io/
Other
80 stars 2 forks source link

Support for cocotb asynchronous IO framework #25

Closed chevillotn closed 9 months ago

chevillotn commented 10 months ago

Hi,

I am trying to port synchronous python code to asynchronous with the minimum amount of effort. This code contains about thousand functions with thousand calls which make it annoying to properly port as this requires adding async/await keywords everywhere.

greenback framework looks like to be exactly what i need. I've tested it using asyncio and that fulfills my needs. However i want to use this not with trio or asyncio but with cocotb which is also an asynchronous IO framework but running in a special HDL simulator, in this case moselsim/questasim. This is for embedded electronics development on FPGAs.

When using greenback as described in the documentation i get the early complaint that it is not supported.

I am wondering how much effort it is to enable support of a new asynchronous IO framework for greenback and what that means exactly. I would assume cocotb is very closely related to asyncio but that's only an assumption. cocotb documentation/sources can be found here: https://www.cocotb.org/ https://github.com/cocotb/cocotb

If you can provide some help would be very useful. Thanks Nicolas

smurfix commented 10 months ago

At first glance that should be reasonably straightforward.

The current task is in cocotb.scheduler._current_task and the coroutine is in task._coro. You don't seem to need much more than that, though I suspect the devil in in the details …

Also, you should send a PR to cocotb to support sniffio.

chevillotn commented 10 months ago

Thanks for the reply. I will do some tests on my side with sniffio and local cocotb.

chevillotn commented 10 months ago

I have patched cocotb to inform sniffio what the task is and patched also greenback to support cocotb similar to how asyncio is implemented. The good thing is that i can use the greenback feature now with cocotb! (with patches...)

So indeed it was quite straight forward, very neat implementation you did here!

I will raise an issue to cocotb to implement the sniffio and current_task such that something can be done on greenback side if you agree?

chevillotn commented 10 months ago

I have opened an issue with cocotb: https://github.com/cocotb/cocotb/issues/3650

chevillotn commented 9 months ago

Hi, Actualluy cocotb already provides a mechanism to do exactly the same as what greenback does. So i have been using this instead and that works fine. Therefore there is no need to add cocotb support to greenback. Thanls for your support!