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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • python

    #1

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

    at line "for j in linkReturned:" , raise an error:
    File "C:\pythonProgr am\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(thr eading.Thread):
    def __init__(self, urlList):
    threading.Threa d.__init__(self )
    urllist=[]
    self.urllist=ur lList
    def run(self):
    urllink=[]
    ......
    return urllink


    for i in range(0,2):
    thread=PrintThr ead(links)
    threadList.appe nd(thread)
    linkReturned=[]
    for i in threadList:
    linkReturned=i. start()
    for j in linkReturned:
    links.append(j)

  • BJörn Lindqvist

    #2
    Re: iteration over non-sequence ,how can I resolve it?

    On 28 May 2006 06:20:20 -0700, python <dongdonglove8@ hotmail.com> wrote:[color=blue]
    > at line "for j in linkReturned:" , raise an error:
    > File "C:\pythonProgr am\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() ?[/color]

    You can't. thread.start() always return None.
    [color=blue]
    > class PrintThread(thr eading.Thread):
    > def __init__(self, urlList):
    > threading.Threa d.__init__(self )
    > urllist=[]
    > self.urllist=ur lList
    > def run(self):
    > urllink=[]
    > ......
    > return urllink
    >
    >
    > for i in range(0,2):
    > thread=PrintThr ead(links)
    > threadList.appe nd(thread)
    > linkReturned=[]
    > for i in threadList:
    > linkReturned=i. start()
    > for j in linkReturned:
    > links.append(j)[/color]
    [color=blue]
    >From the looks of this code it seems like you want a sub-routine not a[/color]
    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

    Comment

    • python

      #3
      Re: iteration over non-sequence ,how can I resolve it?

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

      Comment

      • python

        #4
        Re: iteration over non-sequence ,how can I resolve it?

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

        Comment

        • Serge Orlov

          #5
          Re: iteration over non-sequence ,how can I resolve it?

          python wrote:[color=blue]
          > To BJörn Lindqvist :
          > thank you . how to write the code specifically ?Could you give a
          > example?[/color]

          Use Queue module:

          import threading
          from Queue import Queue

          class PrintThread(thr eading.Thread):
          def __init__(self, urlList, results_queue):
          threading.Threa d.__init__(self )
          urllist=[]
          self.urllist=ur lList
          self.results_qu eue = results_queue
          def run(self):
          urllink=[self.urllist] * 2
          self.results_qu eue.put(urllink )

          results = Queue()
          threadList = []
          for i in range(0,2):
          thread=PrintThr ead("Thread"+st r(i), results)
          threadList.appe nd(thread)
          thread.start()

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

          Comment

          Working...