Re: [cx-oracle-users] passing a connection to a process?
Brought to you by:
atuining
From: Amaury F. d'A. <ama...@gm...> - 2010-06-20 20:40:32
|
2010/6/20 Chuck White <chu...@ch...>: > Thanks for your response. > > Can you please post an example with the multiprocessing module. My understanding is that objects have to be picklable to spawn processes using multi-processing. Maybe I am missing something here. The parameters have to be picklable, yes. But not the state of the worker process... In the following sample, the "connection" object would be a cx_Oracle connection. The important thing is that it's a global variable, so reused the next time the worker process gets a new task. connection = None def worker(param): import time time.sleep(0.1) global connection if connection is None: print "%d Connect" % param connection = param else: print "%d Connected by %d" % (param, connection) return param + 10 if __name__ == '__main__': import multiprocessing pool = multiprocessing.Pool(processes=4) results = [] for x in range(10): def callback(result, x=x): results.append((x, result)) pool.apply_async(worker, [x], callback=callback) pool.close() pool.join() print "results", results -- Amaury Forgeot d'Arc |