import concurrent.futures import itertools import socket import sys from threading import Lock from time import time SERVER = "159.69.4.2" PORT = 1337 NUM_WORKERS = 100 start = time() lock = Lock() count = itertools.count() def worker(): while True: try: with lock: i = next(count) with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: sock.connect((SERVER, PORT)) data = str(i).encode("ascii") sock.sendall(data) response = sock.recv(1024) print(int(response.decode("ascii")), float(time() - start)) except KeyboardInterrupt: break except Exception as e: print(e, file=sys.stderr) with concurrent.futures.ThreadPoolExecutor(max_workers=NUM_WORKERS) as executor: for _ in range(NUM_WORKERS): executor.submit(worker)