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?       ‍

Attachment: 08221DA3@7B7BB14B.140BD253.PNG
Description: Binary data

-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to