我写了一段多处理代码。连接到我正在运行 32 个查询以获取数据的 cassandra。我试图使用 python 中的多处理库来并行化获取。代码看起来像这样。
from cassandra.cluster import Cluster
cluster = Cluster(['xyz'])
session = cluster.connect()
query = session.prepare('SELECT stuff')
session.default_timeout = 600000
session.default_fetch_size = 100
queries = [
session.execute_async(query, ['2021-10-19'] + [i])
for i in range(32)
]
pool = mp.Pool(32)
inter_obj = pool.map_async(compute, queries)
inter_obj.wait()
res = inter_obj.get()
pool.close()
pool.join()
final_response = reduce(aggregate, res)
resp = json.dumps(final_response, sort_keys=True, indent=4).encode("utf-8")
print("RESPONSE", resp)
在运行程序时,它在 wait() 上出错
Traceback (most recent call last):
File "/usr/local/bin/date-run", line 8, in <module>
sys.exit(main())
File "/usr/local/lib/python3.8/dist-packages/sc_eol/run_stuff.py", line 75, in main
res = inter_obj.get()
File "/usr/lib/python3.8/multiprocessing/pool.py", line 768, in get
raise self._value
File "/usr/lib/python3.8/multiprocessing/pool.py", line 537, in _handle_tasks
put(task)
File "/usr/lib/python3.8/multiprocessing/connection.py", line 206, in send
self._send_bytes(_ForkingPickler.dumps(obj))
File "/usr/lib/python3.8/multiprocessing/reduction.py", line 51, in dumps
cls(buf, protocol).dump(obj)
TypeError: cannot pickle '_thread.RLock' object