python - Object inside a threaded multiprocess is empty -
i have odd situation here. have multiprocess in turn has thread. idea here offload work of appending queue thread (i'm trying cpu usage per python process).
anyways, here's odd part. i'm appending results deque, , inside thread attempting send through queue. however, thread not see results appended deque, though object id's same.
here code/output:
class queueappender(thread): def __init__(self, queue=none, read_deque=none, *args, **kwargs): super(queueappender, self).__init__(*args, **kwargs) self.queue = queue self.contents = read_deque def run(self): while true: print 'queue', id(self.contents), len(self.contents) if self.contents: results = self.contents.popleft() if results none: return self.queue.put(results) else: time.sleep(0.01) class worker(process): def __init__(self, queue=none, results=none): super(worker, self).__init__() self.queue=queue self.results = results # queue self.read_deque = deque() self.queuethread = queueappender(queue=self.results, read_deque=self.read_deque) self.queuethread.start() def run(self): results = self.results get_func = self.queue.get reads = get_func() while reads not none: result_batch = [] read in reads: # work here result_batch.append(read) self.read_deque.append(result_batch) print 'worker', id(self.read_deque), len(self.read_deque) reads = get_func() self.read_deque.append(none) self.t.join()
the odd part output:
worker 140413597195744 6 queue 140413597195744 0 queue 140413597195744 0 queue 140413597195744 0 worker 140413597195744 7 queue 140413597195744 0 queue 140413597195744 0 queue 140413597195744 0
to me, says both accessing same memory, worker sees items in it, wheras queue sees 0 items.
any ideas why occurring?
the issue thread being created in init. moving creation run process solved this.
Comments
Post a Comment