-1

I have a new project that I plan to use aiohttp withuWSGI. I found out that uWSGI support asyncio via greentlet. I also did some research about about greenlet and found out it provides event loop too. So as a newbie in python (come from Node.js world). I have a couple of questions about asynchronous programming in python.

If asyncio provides event loop why I have to use greenlet as suspend/resume engine to integrated aiohttp with uWSGI? asyncio doesn't provide suspend/resume engine?

In order to make pyhton act like a Node.js system it need to use combine loop engine, suspend/resume engine and I/O engine (as Node.js use libuv)?

I took a look at this Differences between gevent and tornado and the answer mentioned they are different. as the answer said gevent use coroutine and asyncio provides coroutine too does this mean gevent use the same technique with asyncio?

Could greenlet and asyncio use interchangeably? Or use both?

Andrew Svetlov
  • 16,730
  • 8
  • 66
  • 69
Semooze
  • 143
  • 10

1 Answers1

2

If asyncio provides event loop why I have to use greenlet as suspend/resume engine to integrated aiohttp with uWSGI? asyncio doesn't provide suspend/resume engine ?

Asyncio does provide an event loop with suspendable coroutines, but uWSGI is a complex library that actually predates asyncio. Asyncio's suspend/resume is based on Python 3 async/await, which must be used across the board for a library to be asyncio compliant. (aiohttp is an example of such a library, and there are many others.)

At the cost of some portability, greenlet allows suspending arbitrary synchronous code, which makes it a perfect low-cost solution for bridging uwsgi and asyncio.

as the answer said gevent use coroutine and asyncio provides coroutine too does this mean gevent use the same technique with asyncio ?

gevent also uses greenlets to provide a sequential-like programming experience on top of an asynchronous callback-based system. While asyncio achieves the same goal, it does so using completely different underlying mechanisms, and this prohibits running code written for one under the other. The two can likely be bridged, but don't expect code written for asyncio to "just work" under gevent and (especially) vice versa.

Could greenlet and asyncio use interchangeably ? or use both ?

The two work at different levels: greenlet provides suspendable coroutines without an async event loop, comparable to Python 3 async/await. Asyncio implements a toolbox for async programming that includes an event loop with support for transports and sockets, a coroutine scheduler, and an implementation of Future to integrate with classic callback-based programming. As such, you can compare asyncio to gevent, not to greenlet.

user4815162342
  • 141,790
  • 18
  • 296
  • 355