66import subprocess
77import multiprocessing as mp
88import platform
9- from typing import Any , Dict , Optional
9+ from typing import Any , Dict , Optional , Callable
1010from functools import partial
1111
1212import trio
3030from .log import get_logger
3131from ._portal import Portal
3232from ._actor import Actor , ActorFailure
33- from ._entry import _mp_main , _trio_main
33+ from ._entry import _mp_main
3434
3535
3636log = get_logger ('tractor' )
@@ -158,8 +158,14 @@ async def cancel_on_completion(
158158
159159
160160@asynccontextmanager
161- async def run_in_process (subactor , async_fn , * args , ** kwargs ):
162- encoded_job = cloudpickle .dumps (partial (async_fn , * args , ** kwargs ))
161+ async def run_in_process (
162+ subactor : 'Actor' ,
163+ async_fn : Callable ,
164+ ** kwargs
165+ ):
166+ encoded_blob = cloudpickle .dumps (
167+ (subactor , async_fn , kwargs ,)
168+ )
163169
164170 async with await trio .open_process (
165171 [
@@ -176,7 +182,7 @@ async def run_in_process(subactor, async_fn, *args, **kwargs):
176182 ) as proc :
177183
178184 # send func object to call in child
179- await proc .stdin .send_all (encoded_job )
185+ await proc .stdin .send_all (encoded_blob )
180186 yield proc
181187
182188
@@ -189,6 +195,7 @@ async def new_proc(
189195 bind_addr : Tuple [str , int ],
190196 parent_addr : Tuple [str , int ],
191197 use_trio_run_in_process : bool = False ,
198+ infect_asyncio : bool = False ,
192199 task_status : TaskStatus [Portal ] = trio .TASK_STATUS_IGNORED
193200) -> None :
194201 """Create a new ``multiprocessing.Process`` using the
@@ -203,10 +210,13 @@ async def new_proc(
203210 if use_trio_run_in_process or _spawn_method == 'trio' :
204211 async with run_in_process (
205212 subactor ,
206- _trio_main ,
207- subactor ,
208- bind_addr ,
209- parent_addr ,
213+ partial (
214+ subactor ._async_main ,
215+ accept_addr = bind_addr ,
216+ parent_addr = parent_addr ,
217+ ),
218+ # kwargs bundeled as last pickled tuple element
219+ infect_asyncio = infect_asyncio
210220 ) as proc :
211221 log .info (f"Started { proc } " )
212222
@@ -274,6 +284,7 @@ async def new_proc(
274284 fs_info ,
275285 start_method ,
276286 parent_addr ,
287+ infect_asyncio ,
277288 ),
278289 # daemon=True,
279290 name = name ,
0 commit comments