def hello(sec): print("Yawn") try: thredo.sleep(sec) print("Awake") except thredo.ThreadCancelled: print("Cancelled!")
def func(): print('Yawn') try: thredo.sleep(10) print('Awake') except thredo.ThreadCancelled: print('Cancelled')
def consumer(q): while True: item = q.get() if item is None: break print("Child:", item) thredo.sleep(0.01)
def main(): serv = thredo.spawn(server, ('', 25000)) client = thredo.spawn(test_client, ('localhost', 25000)) thredo.sleep(0.1) client.cancel() start.set() serv.join()
def main(): lck = thredo.Semaphore() t1 = thredo.spawn(func, lck, 'thread-1') t2 = thredo.spawn(func, lck, 'thread-2') # Case 2: Cancel a thread holding a lock thredo.sleep(2) t1.cancel() t2.join()
def producer(): nonlocal n while n < 5: thredo.sleep(0.1) with lock: n += 1 result.append(('produce', n)) lock.notify()
def friend(name): print('Hi, my name is', name) print('Playing Minecraft') try: thredo.sleep(1000) except thredo.CancelledError: print(name, 'going home') raise
def test_client(address): evt.wait() sock = socket(AF_INET, SOCK_STREAM) sock.connect(address) sock.recv(8) sock.send(b'Msg1\nMsg2\n') thredo.sleep(1) sock.send(b'Msg3\n') sock.close()
def main(): # thredo.spawn(spin, 60_000_000) q = thredo.Queue() with thredo.ThreadGroup(wait=None) as g: g.spawn(spin,100_000_000) for n in range(4): g.spawn(worker, q, f'Worker-{n}') for n in range(10): q.put(n) thredo.sleep(1) q.join() print('Done')
def server(address): sock = socket(AF_INET, SOCK_STREAM) sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, True) sock.bind(address) sock.listen(1) evt.set() client, addr = sock.accept() try: client.send(b'OK') s = client.as_stream() thredo.sleep(1) results.append(s.readall()) finally: client.close() sock.close()
def kid(): while True: try: print('Can I play?') thredo.timeout_after(1, start_evt.wait) break except thredo.ThreadTimeout: print('Wha!?!') print('Building the Millenium Falcon in Minecraft') with thredo.ThreadGroup() as f: f.spawn(friend, 'Max') f.spawn(friend, 'Lillian') f.spawn(friend, 'Thomas') try: thredo.sleep(1000) except thredo.CancelledError: print('Fine. Saving my work.') raise
def parent(): goodbye = thredo.SignalEvent(signal.SIGINT, signal.SIGTERM) kid_task = thredo.spawn(kid) thredo.sleep(5) print("Yes, go play") start_evt.set() goodbye.wait() del goodbye print("Let's go") count_task = thredo.spawn(countdown, 10) count_task.join() print("We're leaving!") try: thredo.timeout_after(10, kid_task.join) except thredo.ThreadTimeout: kid_task.cancel() thredo.sleep(3) print('Parent Leaving')
def philosopher(n): thredo.sleep(random.random()) try: with sticks[n]: thredo.sleep(random.random()) with sticks[(n + 1) % 5]: print("eating", n) thredo.sleep(random.random()) except thredo.ThreadCancelled: print(f"But what is death? {n}")
def main(): with thredo.ignore_after(0.25): thredo.sleep(1)
def countdown(n): while n > 0: print('T-minus', n) thredo.sleep(1) n -= 1
def child(): try: thredo.sleep(10) except thredo.ThreadCancelled: result.append('cancel')
def main(): t = thredo.spawn(child) thredo.sleep(0.1) t.cancel()
def main(): try: with thredo.timeout_after(0.25): thredo.sleep(1) except thredo.ThreadTimeout: result.append('timeout')
def main(): t = thredo.spawn(hello, 100) thredo.sleep(5) t.cancel() print("Goodbye")
def main(): q = thredo.Queue(maxsize=1) t = thredo.spawn(producer, q) results.append('start') thredo.sleep(0.1) t.cancel()
def killer(delay, t): thredo.sleep(delay) t.cancel()
def func(lck, label): print(label, 'starting') with lck: print(label, 'working') thredo.sleep(5) print(label, 'done')
def main(): t = thredo.spawn(func) thredo.sleep(2) t.cancel()
def main(): start = time.time() thredo.sleep(1.0) end = time.time() result.append(end - start)