problem on multi-threading

水静流深 1248283536 at qq.com
Fri Jul 25 03:45:24 EDT 2014


The right program is as following,everything is ok.
import requests
import threading
import queue

class  webdata(object):
    def  __init__(self,name):
        self.jobs=queue.Queue()
        for  x  in  name:
            url='http://stockhtm.finance.qq.com/sstock/ggcx/%s.shtml'  %x
            self.jobs.put(url)
    def  download(self):
        while not self.jobs.empty():
            try:
                url = self.jobs.get()
                print(requests.get(url).status_code,url)
                print(threading.currentThread())              
                self.jobs.task_done()
            except:
                print("wrong",url)
                self.jobs.task_done()
    def  run(self):
         for i in range(3):
              threading.Thread(target=self.download).start()
        self.jobs.join()
         print("i am over")


name=['600000', '000001', '600319', '600531','600661', '600983', '600202', '600149']
x=webdata(name)
x.run()‍

question1:

when i delete the line of `self.jobs.join()‍` ,the program will never finished ,why? 

import requests
import threading
import queue

class  webdata(object):
    def  __init__(self,name):
        self.jobs=queue.Queue()
        for  x  in  name:
            url='http://stockhtm.finance.qq.com/sstock/ggcx/%s.shtml'  %x
            self.jobs.put(url)
    def  download(self):
        while not self.jobs.empty():
            try:
                url = self.jobs.get()
                print(requests.get(url).status_code,url)
                print(threading.currentThread())              
                self.jobs.task_done()
            except:
                print("wrong",url)
                self.jobs.task_done()
    def  run(self):
         for i in range(3):
              threading.Thread(target=self.download).start()
        self.jobs.join()
         print("i am over")


name=['600000', '000001', '600319', '600531','600661', '600983', '600202', '600149']
x=webdata(name)
x.run()‍


never quit from the thread ,why?

question2:


I can get every son-thread thread number with   `print(threading.currentThread())  `,how can i get the main thread number in the program?       ‍
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20140725/f2281bd1/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 08221DA3 at 7B7BB14B.140BD253.PNG
Type: application/octet-stream
Size: 24971 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20140725/f2281bd1/attachment.obj>


More information about the Python-list mailing list