iteration over non-sequence ,how can I resolve it?

P

python

at line "for j in linkReturned:" , raise an error:
File "C:\pythonProgram\test.py", line 308, in main
for j in linkReturned:
TypeError: iteration over non-sequence
how can I get a list from the return of thread.start() ?
below is the codes:

class PrintThread(threading.Thread):
def __init__(self, urlList):
threading.Thread.__init__(self)
urllist=[]
self.urllist=urlList
def run(self):
urllink=[]
......
return urllink


for i in range(0,2):
thread=PrintThread(links)
threadList.append(thread)
linkReturned=[]
for i in threadList:
linkReturned=i.start()
for j in linkReturned:
links.append(j)
 
?

=?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=

at line "for j in linkReturned:" , raise an error:
File "C:\pythonProgram\test.py", line 308, in main
for j in linkReturned:
TypeError: iteration over non-sequence
how can I get a list from the return of thread.start() ?

You can't. thread.start() always return None.
class PrintThread(threading.Thread):
def __init__(self, urlList):
threading.Thread.__init__(self)
urllist=[]
self.urllist=urlList
def run(self):
urllink=[]
......
return urllink


for i in range(0,2):
thread=PrintThread(links)
threadList.append(thread)
linkReturned=[]
for i in threadList:
linkReturned=i.start()
for j in linkReturned:
links.append(j)
From the looks of this code it seems like you want a sub-routine not a
thread. You can simulate returning a value from a thread by adding a
"return value" attribute to the PrintThread class that the run()
method writes to. Then you would have to add some form of
synchronizing so that your main program does not try to read the
"return value" of the thread before the thread actually has written
the "return value."
 
P

python

To BJörn Lindqvist :
thank you . how to write the code specifically ?Could you give a
example?
 
P

python

To BJörn Lindqvist :
thank you . how to write the code specifically ?Could you give an
example?
 
S

Serge Orlov

python said:
To BJörn Lindqvist :
thank you . how to write the code specifically ?Could you give a
example?

Use Queue module:

import threading
from Queue import Queue

class PrintThread(threading.Thread):
def __init__(self, urlList, results_queue):
threading.Thread.__init__(self)
urllist=[]
self.urllist=urlList
self.results_queue = results_queue
def run(self):
urllink=[self.urllist] * 2
self.results_queue.put(urllink)

results = Queue()
threadList = []
for i in range(0,2):
thread=PrintThread("Thread"+str(i), results)
threadList.append(thread)
thread.start()

for i in threadList:
linkReturned = results.get()
for j in linkReturned:
print j
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top