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

BJörn Lindqvist bjourne at gmail.com
Sun May 28 10:19:47 EDT 2006


On 28 May 2006 06:20:20 -0700, python <dongdonglove8 at hotmail.com> wrote:
> 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."

-- 
mvh Björn



More information about the Python-list mailing list