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.
@@ -39,13 +40,15 @@ async def run_receiver_task(
3940
4041 :param broker: current broker instance.
4142 :param receiver_cls: receiver class to use.
42- :param sync_workers: number of threads of a threadpool that runs sync tasks.
43+ :param sync_workers: number of threads of a threadpool
44+ or processes in processpool that runs sync tasks.
4345 :param validate_params: whether to validate params or not.
4446 :param max_async_tasks: maximum number of simultaneous async tasks.
4547 :param max_prefetch: maximum number of tasks to prefetch.
4648 :param propagate_exceptions: whether to propagate exceptions in generators or not.
4749 :param run_startup: whether to run startup function or not.
4850 :param ack_time: acknowledge type to use.
51+ :param use_process_pool: whether to use process pool or threadpool.
4952 :raises asyncio.CancelledError: if the task was cancelled.
5053 """
5154 finish_event = asyncio .Event ()
@@ -62,7 +65,12 @@ def on_exit(_: Receiver) -> None:
6265 finish_event .set ()
6366 raise asyncio .CancelledError
6467
65- with ThreadPoolExecutor (max_workers = sync_workers ) as executor :
68+ executor : Executor
69+ if use_process_pool :
70+ executor = ProcessPoolExecutor (max_workers = sync_workers )
71+ else :
72+ executor = ThreadPoolExecutor (max_workers = sync_workers )
73+ with executor as executor :
6674 broker .is_worker_process = True
6775 while True :
6876 try :
0 commit comments