Threads¶
threads.py¶
1import threading
2from zcached import ZCached, Result
3
4
5def worker(worker_id: int, zcached: ZCached) -> None:
6 result: Result[str] = zcached.ping()
7 print(f"Worker {worker_id} | {result.value}")
8
9
10client = ZCached(host="localhost", port=1234, pool_size=2)
11client.run()
12
13if not client.is_alive():
14 raise RuntimeError("Something went wrong :(")
15
16threads = [threading.Thread(target=worker, args=(x, client)) for x in range(10)]
17for thread in threads:
18 thread.start()
19
20
21# There is also a second, easier way. However, this one has limitations.
22"""
23for w_id in range(10):
24 client.connection_pool.run_in_thread(func=worker, worker_id=w_id, zcached=client)
25"""