Skip to content

Commit bcf10e5

Browse files
change multiprocess method back to default (fork)
1 parent 189ed44 commit bcf10e5

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

pedantic/decorators/fn_deco_in_subprocess.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@
44
from typing import Callable, TypeVar, Any, Awaitable, Optional, Type, Union
55

66
try:
7-
import multiprocess as mp
87
from multiprocess import Process, Pipe
98
from multiprocess.connection import Connection
10-
11-
mp.set_start_method(method="spawn", force=True)
129
except ImportError:
1310
Process: Optional[Type] = None
1411
Pipe: Optional[Type] = None
@@ -106,11 +103,18 @@ async def calculate_in_subprocess(func: Callable[..., Union[T, Awaitable[T]]], *
106103
return result
107104

108105

109-
def _inner(tx: Connection, fun: Callable[..., Union[T, Awaitable[T]]], *a, **kw_args) -> None:
106+
def _inner(tx: Connection, fun: Callable[..., Union[T, Awaitable[T]]], *a, new_thread: bool = False, **kw_args) -> None:
110107
""" This runs in another process. """
111108

112109
event_loop = None
113110
if inspect.iscoroutinefunction(fun):
111+
if not new_thread: # see https://stackoverflow.com/a/79785720/10975692
112+
import threading
113+
t = threading.Thread(target=_inner, args=(tx, fun, *a), kwargs=(kw_args | {"new_thread": True}))
114+
t.start()
115+
t.join()
116+
return
117+
114118
event_loop = asyncio.new_event_loop()
115119
asyncio.set_event_loop(event_loop)
116120

0 commit comments

Comments
 (0)