11import asyncio
2- from concurrent .futures import ThreadPoolExecutor
2+ from concurrent .futures import Executor , ProcessPoolExecutor , ThreadPoolExecutor
33from logging import getLogger
44from typing import Optional , Type
55
1313async def run_receiver_task (
1414 broker : AsyncBroker ,
1515 receiver_cls : Type [Receiver ] = Receiver ,
16- sync_workers : int = 4 ,
16+ sync_workers : Optional [ int ] = None ,
1717 validate_params : bool = True ,
1818 max_async_tasks : int = 100 ,
1919 max_prefetch : int = 0 ,
2020 propagate_exceptions : bool = True ,
2121 run_startup : bool = False ,
2222 ack_time : Optional [AcknowledgeType ] = None ,
23+ use_process_pool : bool = False ,
2324) -> None :
2425 """
2526 Function to run receiver programmatically.
@@ -46,6 +47,7 @@ async def run_receiver_task(
4647 :param propagate_exceptions: whether to propagate exceptions in generators or not.
4748 :param run_startup: whether to run startup function or not.
4849 :param ack_time: acknowledge type to use.
50+ :param use_process_pool: whether to use process pool or threadpool.
4951 :raises asyncio.CancelledError: if the task was cancelled.
5052 """
5153 finish_event = asyncio .Event ()
@@ -62,7 +64,12 @@ def on_exit(_: Receiver) -> None:
6264 finish_event .set ()
6365 raise asyncio .CancelledError
6466
65- with ThreadPoolExecutor (max_workers = sync_workers ) as executor :
67+ executor : Executor
68+ if use_process_pool :
69+ executor = ProcessPoolExecutor (max_workers = sync_workers )
70+ else :
71+ executor = ThreadPoolExecutor (max_workers = sync_workers )
72+ with executor as executor :
6673 broker .is_worker_process = True
6774 while True :
6875 try :
0 commit comments