Disable shm resource tracker via flag on 3.13+
As per the newly added support, https://docs.python.org/3/library/multiprocessing.shared_memory.htmlmain
parent
79f502034f
commit
5021514a6a
|
@ -17,20 +17,38 @@
|
|||
Utils to tame mp non-SC madeness
|
||||
|
||||
'''
|
||||
import platform
|
||||
|
||||
|
||||
# !TODO! in 3.13 this can be disabled (the-same/similarly) using
|
||||
# a flag,
|
||||
# - [ ] soo if it works like this, drop this module entirely for
|
||||
# 3.13+ B)
|
||||
# |_https://docs.python.org/3/library/multiprocessing.shared_memory.html
|
||||
#
|
||||
def disable_mantracker():
|
||||
'''
|
||||
Disable all `multiprocessing` "resource tracking" machinery since
|
||||
it's an absolute multi-threaded mess of non-SC madness.
|
||||
|
||||
'''
|
||||
from multiprocessing import resource_tracker as mantracker
|
||||
from multiprocessing.shared_memory import SharedMemory
|
||||
|
||||
|
||||
# 3.13+ only.. can pass `track=False` to disable
|
||||
# all the resource tracker bs.
|
||||
# https://docs.python.org/3/library/multiprocessing.shared_memory.html
|
||||
if (_py_313 := (
|
||||
platform.python_version_tuple()[:-1]
|
||||
>=
|
||||
('3', '13')
|
||||
)
|
||||
):
|
||||
from functools import partial
|
||||
return partial(
|
||||
SharedMemory,
|
||||
track=False,
|
||||
)
|
||||
|
||||
# !TODO, once we drop 3.12- we can obvi remove all this!
|
||||
else:
|
||||
from multiprocessing import (
|
||||
resource_tracker as mantracker,
|
||||
)
|
||||
|
||||
# Tell the "resource tracker" thing to fuck off.
|
||||
class ManTracker(mantracker.ResourceTracker):
|
||||
|
@ -50,3 +68,8 @@ def disable_mantracker():
|
|||
mantracker.ensure_running = mantracker._resource_tracker.ensure_running
|
||||
mantracker.unregister = mantracker._resource_tracker.unregister
|
||||
mantracker.getfd = mantracker._resource_tracker.getfd
|
||||
|
||||
# use std type verbatim
|
||||
shmT = SharedMemory
|
||||
|
||||
return shmT
|
||||
|
|
|
@ -23,14 +23,15 @@ considered optional within the context of this runtime-library.
|
|||
|
||||
"""
|
||||
from __future__ import annotations
|
||||
from multiprocessing import shared_memory as shm
|
||||
from multiprocessing.shared_memory import (
|
||||
# SharedMemory,
|
||||
ShareableList,
|
||||
)
|
||||
import platform
|
||||
from sys import byteorder
|
||||
import time
|
||||
from typing import Optional
|
||||
from multiprocessing import shared_memory as shm
|
||||
from multiprocessing.shared_memory import (
|
||||
SharedMemory,
|
||||
ShareableList,
|
||||
)
|
||||
|
||||
from msgspec import (
|
||||
Struct,
|
||||
|
@ -61,7 +62,7 @@ except ImportError:
|
|||
log = get_logger(__name__)
|
||||
|
||||
|
||||
disable_mantracker()
|
||||
SharedMemory = disable_mantracker()
|
||||
|
||||
|
||||
class SharedInt:
|
||||
|
@ -797,7 +798,14 @@ def open_shm_list(
|
|||
# "close" attached shm on actor teardown
|
||||
try:
|
||||
actor = tractor.current_actor()
|
||||
|
||||
actor.lifetime_stack.callback(shml.shm.close)
|
||||
|
||||
# XXX on 3.13+ we don't need to call this?
|
||||
# -> bc we pass `track=False` for `SharedMemeory` orr?
|
||||
if (
|
||||
platform.python_version_tuple()[:-1] < ('3', '13')
|
||||
):
|
||||
actor.lifetime_stack.callback(shml.shm.unlink)
|
||||
except RuntimeError:
|
||||
log.warning('tractor runtime not active, skipping teardown steps')
|
||||
|
|
Loading…
Reference in New Issue