def test_rlock_acquire_interruption(self): import _thread, signal, time # Mimic receiving a SIGINT (KeyboardInterrupt) with SIGALRM while stuck # in a deadlock. # XXX this test can fail when the legacy (non-semaphore) implementation # of locks is used in thread_pthread.h, see issue #11223. oldalrm = signal.signal(signal.SIGALRM, self.alarm_interrupt) try: rlock = _thread.RLock() # For reentrant locks, the initial acquisition must be in another # thread. def other_thread(): rlock.acquire() _thread.start_new_thread(other_thread, ()) # Wait until we can't acquire it without blocking... while rlock.acquire(blocking=False): rlock.release() time.sleep(0.01) signal.alarm(1) t1 = time.time() #raises(KeyboardInterrupt, rlock.acquire, timeout=5) try: rlock.acquire(timeout=5) except KeyboardInterrupt: pass else: assert False, 'Expected KeyboardInterrupt' dt = time.time() - t1 # See rationale above in test_lock_acquire_interruption assert dt < 3.0 finally: signal.signal(signal.SIGALRM, oldalrm)
def test_release_save(self): import _thread lock = _thread.RLock() lock.acquire() state = lock._release_save() lock._acquire_restore(state) lock.release()
def test_rlock_acquire_interruption(self): # Mimic receiving a SIGINT (KeyboardInterrupt) with SIGALRM while stuck # in a deadlock. # XXX this test can fail when the legacy (non-semaphore) implementation # of locks is used in thread_pthread.h, see issue #11223. oldalrm = signal.signal(signal.SIGALRM, self.alarm_interrupt) try: rlock = thread.RLock() # For reentrant locks, the initial acquisition must be in another # thread. def other_thread(): rlock.acquire() with threading_helper.wait_threads_exit(): thread.start_new_thread(other_thread, ()) # Wait until we can't acquire it without blocking... while rlock.acquire(blocking=False): rlock.release() time.sleep(0.01) signal.alarm(1) t1 = time.monotonic() self.assertRaises(KeyboardInterrupt, rlock.acquire, timeout=5) dt = time.monotonic() - t1 # See rationale above in test_lock_acquire_interruption self.assertLess(dt, 3.0) finally: signal.alarm(0) signal.signal(signal.SIGALRM, oldalrm)
def test_release_save(self): import _thread lock = _thread.RLock() raises(RuntimeError, lock._release_save) lock.acquire() state = lock._release_save() lock._acquire_restore(state) lock.release()
def test_reacquire(self): import _thread lock = _thread.RLock() lock.acquire() lock.acquire() lock.release() lock.acquire() lock.release() lock.release()
def test_rlock_repr(self): import _thread rlock = _thread.RLock() assert repr(rlock).startswith( "<unlocked _thread.RLock object owner=0 count=0 at ") rlock.acquire() rlock.acquire() assert repr(rlock).startswith("<locked _thread.RLock object owner=") assert 'owner=0' not in repr(rlock) assert " count=2 at " in repr(rlock)
def test__is_owned(self): import _thread lock = _thread.RLock() assert lock._is_owned() is False lock.acquire() assert lock._is_owned() is True lock.acquire() assert lock._is_owned() is True lock.release() assert lock._is_owned() is True lock.release() assert lock._is_owned() is False
def test_release_unacquired(self): # Cannot release an unacquired lock import _thread lock = _thread.RLock() raises(RuntimeError, lock.release) lock.acquire() lock.acquire() lock.release() lock.acquire() lock.release() lock.release() raises(RuntimeError, lock.release)
def __init__(self, in_port=8001, out_port=8002, password=None, max_buffer=1024): self.__in_port = in_port self.__out_port = out_port self.__socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.__socket.setblocking(False) self.__socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True) self.__socket.bind(("", in_port)) #self.__socket.listen(1) self.__max_buffer = max_buffer self.__connection = None self.__address = None self.__lock = _thread.RLock()
class ThreadPool(): cnt_lock = _thread.RLock() cnt = 0 if os.environ.get(b"ENABLE_THREADED_GRAALPYTEST") == b"true": maxcnt = min(os.cpu_count(), 16) sleep = time.sleep start_new_thread = _thread.start_new_thread print("Running with %d threads" % maxcnt) else: sleep = lambda x: x start_new_thread = lambda f, args: f(*args) maxcnt = 1 @classmethod def start(self, function): self.acquire_token() def runner(): try: function() finally: self.release_token() self.start_new_thread(runner, ()) self.sleep(0.5) @classmethod def acquire_token(self): while True: with self.cnt_lock: if self.cnt < self.maxcnt: self.cnt += 1 break self.sleep(1) @classmethod def release_token(self): with self.cnt_lock: self.cnt -= 1 @classmethod def shutdown(self): self.sleep(2) while self.cnt > 0: self.sleep(2)
def __init__(self, name, host, port): self.names = {} self.entities = {} self.reading = False self.name = name self.host = host self.port = port self.compression = False self.state = Login self.position = [] self.yaw = 0 self.pitch = 0 self.accepted_teleport = 0 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.connect((host, port)) self.lock = _thread.RLock() self.following = None print("MCBot initialized")
def __init__(self, iterable=None, maxlen=None): if maxlen is None: self._maxlen = sys.maxsize elif maxlen >= 0: self._maxlen = maxlen else: raise ValueError("maxlen must be non-negative") self._mutex = _thread.RLock() self._maxlen = sys.maxsize if maxlen is None else maxlen self.leftblock = Block(None, None) self.rightblock = self.leftblock self.leftindex = CENTER + 1 self.rightindex = CENTER self.len = 0 self._lock = None assert self.leftindex > 0 assert self.rightindex > 0 if iterable is not None: self.extend(iterable)
def test_rlock_acquire_retries_on_intr(self): self.acquire_retries_on_intr(thread.RLock())
import _io import sys import time import _thread os = sys.modules.get("posix", sys.modules.get("nt", None)) if os is None: raise ImportError("posix or nt module is required in builtin modules") FAIL = '\033[91m' ENDC = '\033[0m' BOLD = '\033[1m' verbose = False print_lock = _thread.RLock() class ThreadPool(): cnt_lock = _thread.RLock() cnt = 0 if os.environ.get(b"ENABLE_THREADED_GRAALPYTEST") == b"true": maxcnt = min(os.cpu_count(), 16) sleep = time.sleep start_new_thread = _thread.start_new_thread print("Running with %d threads" % maxcnt) else: sleep = lambda x: x start_new_thread = lambda f, args: f(*args) maxcnt = 1
def test(): assert (_weakref.ReferenceType is _weakref.ref) for _ in range(99): time.sleep(5) # Throttle to avoid a MemoryError try: _weakref._remove_dead_weakref(dct, 'obj') except NameError: pass obj = fclass() _weakref.getweakrefcount dct = {'obj': _weakref.ref(obj)} _weakref.getweakrefs(obj) _weakref.getweakrefs(dct['obj']) dct['prox'] = _weakref.proxy(ffunc, ffunc) dct['oprox'] = _weakref.proxy(obj, ffunc) q = _queue.SimpleQueue() lock = rnd.choice([_thread.allocate_lock, _thread.allocate])() def lock_thread(*a, **k): try: rnd.choice([lock.acquire_lock, lock.acquire])(blocking=fbool(), timeout=rnd.randint(-10, 10)) except ValueError: pass rnd.choice([lock.locked, lock.locked_lock])() ffunc(q) try: rnd.choice([lock.release_lock, lock.release])() except RuntimeError: pass try: with lock: ffunc(q) except RuntimeError: pass rnd.choice([lock.locked, lock.locked_lock])() l = _thread._set_sentinel() if fbool(): rnd.choice([_thread.exit_thread, _thread.exit])() rlock = _thread.RLock() def rlock_thread(*a, **k): rlock.acquire(fbool()) ffunc(q) rlock._is_owned() if fbool(): try: rlock._release_save() except RuntimeError: pass if fbool(): rlock._acquire_restore( (rnd.randint(-9999, 9999), rnd.randint(-9999, 9999))) try: rlock.release() except RuntimeError: pass try: with rlock: ffunc(q) except RuntimeError: pass rlock._is_owned() if fbool(): rnd.choice([_thread.exit_thread, _thread.exit])() for _ in range(99): repr(lock) repr(rlock) try: rnd.choice([_thread.start_new, _thread.start_new_thread ])(rnd.choice([lock_thread, rlock_thread]), (q, ) + ftup(), fdict()) except RuntimeError: pass try: _thread.stack_size(rnd.randint(-99999999, 99999999)) except (ValueError, OverflowError): pass ffunc(q) try: _thread.interrupt_main() except KeyboardInterrupt: pass
def test_rlock_acquire_retries_on_intr(self): import _thread self.acquire_retries_on_intr(_thread.RLock())
def test_timeout(self): import _thread lock = _thread.RLock() assert lock.acquire() is True assert lock.acquire(False) is True assert lock.acquire(True, timeout=.1) is True
def test_context_manager(self): import _thread lock = _thread.RLock() with lock: assert lock._is_owned() is True