How to stop multithreading returning combined data using python
When using a multithreading, I get combined data
The list is : A,B,C. If I MT this, the fdata [] contains data from A,B and C. How do I Get fdata too hold only one set of data. I tried del fdata didnt help. I need some kind of lock.class WorkerThread(threading.Thread):
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue
de开发者_运维百科f run(self):
while 1:
try: # take a job from the queue
symbol, test, test2 = self.queue.get_nowait()
except Queue.Empty:
raise SystemExit
fn = %s.CSV" % symbol
fdata = []
fo = open(fn, 'rb')
fr = csv.reader(fo, dialect='excel')
for row in fr:
fdata.append(row)
#print fdata
#del fdata
How would I add the thread number to fdata or list id A,B,C to fdata?
fdata should always contain the contents of the CSV file, after all you did for-loop over the rows, so it should always include A B and C... Maybe you should explain more of what you're trying to do.
As to your second question -
Your thread object has an ident see thread.get_ident()
Return the ‘thread identifier’ of the current thread. This is a nonzero integer. Its value has no direct meaning; it is intended as a magic cookie to be used e.g. to index a dictionary of thread-specific data. Thread identifiers may be recycled when a thread exits and another thread is created.
Edit:
Maybe fdata is somehow a global variable or being saved across accesses to the file? I see nothing in the code snippet that would do that, but I'm at a loss to otherwise explain it. According to the function fdata should be a locally scoped variable in the function, and it should just go away with the stack frame...
精彩评论