19

I have a web app built on a Python 3.5+ async framework (apistar, sanic, etc). The app makes various IO calls - to a database, Redis, etc - which are also async.

Some docs 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) 

It's my understanding that await statements allow the event loop to context switch whenever it hits IO, so additional event loops seem completely unnecessary.

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

2 Answers 2

11

You should use multiple event loops one by one when you run your tests, so each test case is run against its own event loop.

You may have to use multiple loops simultaneously depending on requirements of underlying frameworks. In example pyzmq and quamash require their own event loop.

You may want to use multiple loops simultaneously if you need better control over task execution. In example when you want to explicitly group tasks and decide which group should be executed.

Keep in mind that current implementation allows to run only one loop per thread at a time.

Sign up to request clarification or add additional context in comments.

Comments

5

You should use only one IO loop at once, and only one IO loop is allowed per thread at once. threading and asyncio use different and contrasting approaches to concurrency. Running more than one loop (in more than one thread) is considered a bad practice and should be avoided.

The docs above does not suggest to use an "additional" loop. It shows how to specify explicitly a custom loop without (or before) registering it as the default loop.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.