I read a big file to insert to Cassandra. I tried to create a Cassandra session, and pass it to multiple thread. I wish all thread use same session, to avoid thousands times reconnection.
I found the pool never call insert_chunk function because not see logging. Any idea why the session can't be passed to in a thread? I also tried to make session a global instead of parameter, the insert_chunk be called, but stuck at session.prepare
def insert_chunk(chunk, session):
logging.info("insert chunck started")
prepared = session.prepare(...)
for line in chunck:
session.execute(prepared, values)
main():
cluster = Cluster()
session = cluster.connect()
with open("BIG_JSON", mode="r") as _file:
objects = ijson.items(_file, "item")
with Pool(20) as pool:
while True:
chunk = list(itertools.islice(objects, 10000))
if len(chunk) <= 0:
break
pool.apply_async(
insert_chunk,[chunk, session],
)
pool.close()
pool.join()