Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #75198 > unrolled thread

problem on multi-threading

Started by"水静流深" <1248283536@qq.com>
First post2014-07-25 15:45 +0800
Last post2014-07-25 15:45 +0800
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python


Contents

  problem on multi-threading "水静流深" <1248283536@qq.com> - 2014-07-25 15:45 +0800

#75198 — problem on multi-threading

From"水静流深" <1248283536@qq.com>
Date2014-07-25 15:45 +0800
Subjectproblem on multi-threading
Message-ID<mailman.12313.1406275494.18130.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web