Flow control in a stream.

I came across several flow control examples using the streaming module (using Python 2.6).

I am trying to understand how this example calls the "run" method and where. I can not see anything. The ThreadUrl class receives an instance of the main () function as "t", and it is here that I usually expect the code to start the "run" method.

Maybe this is not the best way to work with threads? Please enlighten me:

#!/usr/bin/env python

import Queue
import time
import urllib2
import threading
import datetime

hosts = ["http://example.com/", "http://www.google.com"]

queue = Queue.Queue()

class ThreadUrl(threading.Thread):
    """Threaded Url Grab"""
    def __init__(self, queue):
            threading.Thread.__init__(self)
            self.queue = queue

    def run(self):
            while True:
                    #grabs host from queue
                    host = self.queue.get()

                    #grabs urls of hosts and prints first 1024 bytes of page
                    url = urllib2.urlopen(host)
                    print url.read(10)

                    #signals to queue job is done
                    self.queue.task_done()

start = time.time()

def main():

    #spawn a pool of threads, and pass them queue instance
    for i in range(1):
            t = ThreadUrl(queue)
            t.setDaemon(True)
            t.start()

            for host in hosts:
                    queue.put(host)

    queue.join()
main()
print "Elapsed time: %s" % (time.time() - start)
+3
source share
3 answers

In pydoc :

Thread.start()

Launch thread activity.

. object run(), .

RuntimeException, , .

python Thread , python, ( run, target), C, , . , start : - , , C, run . , , , . , , - t.run(), , .

+7

run() "threading.Thread" ( Google ). t.start().

threading.py( python). Thread. "start()". start(), '_start_new_thread (self.__ bootstrap,())' , , '__bootstrap()' . '__bootstrap()', , '__bootstrap_inner()', , , 'run()'.

, .: D

+4

t.start() , , run() ( , target Thread)

0

Source: https://habr.com/ru/post/1718196/


All Articles