def test_client(address): evt.wait() sock = socket(AF_INET, SOCK_STREAM) sock.connect(address) sock.recv(8) s = sock.as_stream() try: thredo.timeout_after(0.5, s.writelines, line_generator()) except thredo.ThreadTimeout as e: results.append(e.bytes_written) sock.close()
def test_client(address): evt.wait() sock = socket(AF_INET, SOCK_STREAM) sock.connect(address) sock.recv(8) s = sock.as_stream() try: msg = b'x'*10000000 # Must be big enough to fill buffers thredo.timeout_after(0.5, s.write, msg) except thredo.ThreadTimeout as e: results.append(e.bytes_written) 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 main(): phils = [thredo.spawn(philosopher, n) for n in range(5)] try: with thredo.timeout_after(10): for p in phils: p.wait() except thredo.ThreadTimeout: phils[random.randint(0, 4)].cancel() for p in phils: p.wait()
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') results.append('handler start') s = client.as_stream() try: thredo.timeout_after(0.5, s.readlines) except thredo.ThreadTimeout as e: results.extend(e.lines_read) results.append('handler done') finally: client.close() sock.close()
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 main(): try: with thredo.timeout_after(0.25): thredo.sleep(1) except thredo.ThreadTimeout: result.append('timeout')