PATIO RabbitMQ代理实现
项目描述
PATIO Rabbitmq
PATIO是Python异步异步IO的缩写。
此软件包提供RabbitMQ代理实现。
示例
任务执行器
import asyncio
import operator
from functools import reduce
from patio import Registry, ThreadPoolExecutor
from patio_rabbitmq import RabbitMQBroker
rpc = Registry(project="patio-rabbitmq", auto_naming=False)
@rpc("mul")
def mul(*args):
return reduce(operator.mul, args)
async def main():
async with ThreadPoolExecutor(rpc, max_workers=16) as executor:
async with RabbitMQBroker(
executor, amqp_url="amqp://guest:guest@localhost/",
) as broker:
await broker.join()
if __name__ == "__main__":
asyncio.run(main())
任务生产者
import asyncio
from patio import NullExecutor, Registry
from patio_rabbitmq import RabbitMQBroker
async def main():
async with NullExecutor(Registry(project="patio-rabbitmq")) as executor:
async with RabbitMQBroker(
executor, amqp_url="amqp://guest:guest@localhost/",
) as broker:
print(
await asyncio.gather(
*[
broker.call("mul", i, i, timeout=1) for i in range(10)
]
),
)
if __name__ == "__main__":
asyncio.run(main())