Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Moved class out of function to hopefully enable pickling. #664

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 4 additions & 15 deletions scalene/replacement_mp_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,12 @@
# static PyObject * _multiprocessing_SemLock_acquire_impl(SemLockObject *self, int blocking, PyObject *timeout_obj)
#
# timeout_obj is parsed as a double

from scalene.replacement_sem_lock import ReplacementSemLock

@Scalene.shim
def replacement_mp_semlock(scalene: Scalene) -> None:
class ReplacementSemLock(multiprocessing.synchronize.Lock):
def __enter__(self) -> bool:
timeout = sys.getswitchinterval()
tident = threading.get_ident()
while True:
scalene.set_thread_sleeping(tident)
acquired = self._semlock.acquire(timeout=timeout) # type: ignore
scalene.reset_thread_sleeping(tident)
if acquired:
return True

def __exit__(self, *args: Any) -> None:
super().__exit__(*args)

ReplacementSemLock.__qualname__ = (
"replacement_mp_semlock.ReplacementSemLock"
"replacement_semlock.ReplacementSemLock"
)
multiprocessing.synchronize.Lock = ReplacementSemLock # type: ignore
23 changes: 23 additions & 0 deletions scalene/replacement_sem_lock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import multiprocessing
import sys
import threading
from scalene.scalene_profiler import Scalene
from typing import Any

def _recreate_replacement_sem_lock():
return ReplacementSemLock()

class ReplacementSemLock(multiprocessing.synchronize.Lock):
def __enter__(self) -> bool:
timeout = sys.getswitchinterval()
tident = threading.get_ident()
while True:
Scalene.set_thread_sleeping(tident)
acquired = self._semlock.acquire(timeout=timeout) # type: ignore
Scalene.reset_thread_sleeping(tident)
if acquired:
return True
def __exit__(self, *args: Any) -> None:
super().__exit__(*args)
def __reduce__(self):
return (_recreate_replacement_sem_lock, ())