10 asynchronous threads
- asyncio . Concurrency -.
?
asyncio_redis.Connection.create - , , yield from, :
connection = yield from asyncio_redis.Connection.create(host='127.0.0.1', port=6379)
, , , asyncio. , . asyncio_redis , :
import asyncio
import asyncio_redis
@asyncio.coroutine
def main():
connection = yield from asyncio_redis.Pool.create(host='127.0.0.1', port=6379, poolsize=10)
try:
yield from asyncio.gather(
connection.brpop(['queue:pixel'], timeout=0),
connection.brpop(['queue:pixel'], timeout=0),
connection.brpop(['queue:pixel'], timeout=0),
)
finally:
connection.close()
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
Python 3.5 +
Python 3.5+, .
Upd:
(, , ) : , asyncio. .
run_in_executor :
from concurrent.futures import ProcessPoolExecutor
executor = ProcessPoolExecutor(max_workers=10)
async def worker():
while True:
data = await connection.brpop(['queue'], timeout=0)
print(data)
loop = asyncio.get_event_loop()
result = await loop.run_in_executor(executor, blocking_code, data)