When to use multiple event loops?

I have a web application based on the Python 3.5+ asynchronous structure (apistar, sanic, etc.). The application makes various I / O calls - to the database, Redis, etc., which are also asynchronous.

Some documents recommend using an additional event loop:

import asyncio
import peewee
from peewee_async import Manager, PostgresqlDatabase

loop = asyncio.new_event_loop() # Note: custom loop!
database = PostgresqlDatabase('test')
objects = Manager(database, loop=loop)

I understand that statements awaitallow the event loop to switch to context every time IO hits, so additional event loops seem completely unnecessary.

What is the advantage of using an extra event loop and when should additional loops be used?

+6
source share
2 answers

You must use only one I / O cycle at a time, and only one I / O cycle is allowed for each stream at a time. threadingand asynciouse different and contrasting approaches to concurrency. Running more than one loop (in more than one thread) is considered bad practice and should be avoided.

The above documents do not suggest the use of an “extra” loop. It shows how to explicitly specify a user loop without (or before) registering it as a default loop.

0
source

You should use several event loops one after another when you run your tests, so each test case runs against its own event loop.

, . pyzmq quamash .

, . , , .

, .

0

All Articles