@@ -46,15 +46,13 @@ class MultiProcMapper(map_pb2_grpc.MapServicer):
4646 handler: Function callable following the type signature of MapCallable
4747 sock_path: Path to the TCP port to bind to
4848 max_message_size: The max message size in bytes the server can receive and send
49- max_threads: The max number of threads to be spawned;
50- defaults to number of processors x4
5149
5250 Example invocation:
5351 >>> from typing import Iterator
5452 >>> from pynumaflow.mapper import Messages, Message \
5553 ... Datum, MultiProcMapper
5654 ...
57- >>> def map_handler(key: [str], datum: Datum) -> Messages:
55+ >>> def map_handler(keys: list [str], datum: Datum) -> Messages:
5856 ... val = datum.value
5957 ... _ = datum.event_time
6058 ... _ = datum.watermark
@@ -65,6 +63,15 @@ class MultiProcMapper(map_pb2_grpc.MapServicer):
6563 >>> grpc_server.start()
6664 """
6765
66+ __slots__ = (
67+ "__map_handler" ,
68+ "_max_message_size" ,
69+ "_server_options" ,
70+ "_sock_path" ,
71+ "_process_count" ,
72+ "_threads_per_proc" ,
73+ )
74+
6875 def __init__ (
6976 self ,
7077 handler : MapCallable ,
@@ -81,10 +88,8 @@ def __init__(
8188 ("grpc.so_reuseaddr" , 1 ),
8289 ]
8390 self ._sock_path = sock_path
84- self ._process_count = int (
85- os .getenv ("NUM_CPU_MULTIPROC" ) or os .getenv ("NUMAFLOW_CPU_LIMIT" , 1 )
86- )
87- self ._thread_concurrency = int (os .getenv ("MAX_THREADS" , 0 )) or (self ._process_count * 4 )
91+ self ._process_count = int (os .getenv ("NUM_CPU_MULTIPROC" ) or os .cpu_count ())
92+ self ._threads_per_proc = int (os .getenv ("MAX_THREADS" , "4" ))
8893
8994 def MapFn (
9095 self , request : map_pb2 .MapRequest , context : NumaflowServicerContext
@@ -127,12 +132,16 @@ def IsReady(
127132 """
128133 return map_pb2 .ReadyResponse (ready = True )
129134
130- def _run_server (self , bind_address ) :
135+ def _run_server (self , bind_address : str ) -> None :
131136 """Start a server in a subprocess."""
132- _LOGGER .info ("Starting new server." )
137+ _LOGGER .info (
138+ "Starting new server with num_procs: %s, num_threads/proc: %s" ,
139+ self ._process_count ,
140+ self ._threads_per_proc ,
141+ )
133142 server = grpc .server (
134143 futures .ThreadPoolExecutor (
135- max_workers = self ._thread_concurrency ,
144+ max_workers = self ._threads_per_proc ,
136145 ),
137146 options = self ._server_options ,
138147 )
0 commit comments