Python asynchronous callbacks and generators

前端 未结 3 1852
生来不讨喜
生来不讨喜 2021-02-04 12:13

I\'m trying to convert a synchronous library to use an internal asynchronous IO framework. I have several methods that look like this:

def foo:
  ....
  sync_cal         


        
3条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-04 13:00

    UPDATE: take this with a grain of salt, as I'm out of touch with modern python async developments, including gevent and asyncio and don't actually have serious experience with async code.


    There are 3 common approaches to thread-less async coding in Python:

    1. Callbacks - ugly but workable, Twisted does this well.

    2. Generators - nice but require all your code to follow the style.

    3. Use Python implementation with real tasklets - Stackless (RIP) and greenlet.

    Unfortunately, ideally the whole program should use one style, or things become complicated. If you are OK with your library exposing a fully synchronous interface, you are probably OK, but if you want several calls to your library to work in parallel, especially in parallel with other async code, then you need a common event "reactor" that can work with all the code.

    So if you have (or expect the user to have) other async code in the application, adopting the same model is probably smart.

    If you don't want to understand the whole mess, consider using bad old threads. They are also ugly, but work with everything else.

    If you do want to understand how coroutines might help you - and how they might complicate you, David Beazley's "A Curious Course on Coroutines and Concurrency" is good stuff.

    Greenlets might be actualy the cleanest way if you can use the extension. I don't have any experience with them, so can't say much.

提交回复
热议问题