Python 正在等待线程完成使用联接。相当基本

Python 正在等待线程完成使用联接。相当基本,python,multithreading,join,Python,Multithreading,Join,我有一个非常基本的任务,但是我的主线程在等待我生成的所有其他线程完成时遇到了一个问题 这段代码什么都不做,只是作为一个线程练习 这是我的密码: import time from threading import Thread def printNumbers(lowEnd, highEnd): while(lowEnd <= highEnd): print(repr(lowEnd)) lowEnd += 1 countTo = 100000

我有一个非常基本的任务,但是我的主线程在等待我生成的所有其他线程完成时遇到了一个问题

这段代码什么都不做,只是作为一个线程练习

这是我的密码:

import time
from threading import Thread

def printNumbers(lowEnd, highEnd):
    while(lowEnd <= highEnd):
        print(repr(lowEnd))
        lowEnd += 1


countTo = 100000

#Test using 1 thread.        
startSingleThread = time.clock()
printNumbers(0,countTo)
elapsedSingleThread = (time.clock() - startSingleThread)

#Test using 10 threads
numberOfThreads      = 10
countAmountPerThread = countTo/numberOfThreads

startTenThread = time.clock()
for i in range(numberOfThreads):
    threadLowEnd  = i*countAmountPerThread
    threadHighEnd = (i+1)*countAmountPerThread
    t = Thread(target=printNumbers, args=(threadLowEnd,threadHighEnd,))
    t.start()

#Join all existing threads to main thread.
for thread in threading.enumerate():
    if thread is not threading.currentThread():
        thread.join()

elapsedTenThread = (time.clock() - startTenThread)

print("Time for 1 thread: " + repr(elapsedSingleThread))
print("time for 10 threads: " + repr(elapsedTenThread))
导入时间
从线程导入线程
def打印编号(低端、高端):

while(lowEnd您无法看到stderr,因为您正在向stdout打印太多内容,但您有以下错误:

Traceback (most recent call last):
  File "test.py", line 29, in <module>
    for thread in threading.enumerate():
NameError: name 'threading' is not defined

…这可能是你所期望看到的,因为它发生在所有的数字都被打印出来之后。

我已经与之斗争了一段时间,我找到了一种适合我的方法-它是我在网上找到的东西的集合,现在它是我的主要“模式”当我使用多线程应用程序时。它并不是专门回答你的问题,但它展示了一种以python方式利用标准库的方法。但是,请记住,没有什么比阅读标准库文档更好的了

from threading import Thread, Lock
from Queue import Queue
from datetime import datetime
import time
import random

class Worker(Thread):
    """This is the main worker - it will process jobs as long as the "job
    queue" has jobs available.

    """
    # this lock is used to avoid messing up the screen output - only
    # one worker will write to screen at a given time. It is
    # technically a Mutual Exclusion (mutex)
    screen_mutex = Lock()

    def __init__(self, queue):
        # initialize the base class
        super(Worker, self).__init__()
        self.queue = queue

    def log(self, message):
        """This convenience function is used to print a message to the
        screen. You are better off using the logging module, but
        beware! It is not thread safe (use a server).

        """
        Worker.screen_mutex.acquire()        
        print("{timestamp:%d-%b-%Y %H:%M:%S.%f UTC} "
              "{name}: {message}".format(timestamp=datetime.utcnow(),
                                         name=self.getName(),
                                         message=message))
        Worker.screen_mutex.release()

    def run(self):
        """This is the method called when you start the thread."""
        # The following is an infinite loop which will continue
        # processing jobs as long as there are jobs available in the
        # queue
        while True:
            # this is how you get a job from the queue - this call
            # will block until a job is available, or when the parent
            # thread finishes
            job = self.queue.get()

            # in this case the job is simply a random number
            # indicating how many seconds to sleep (typical example)
            self.log("sleeping for {0} seconds".format(job))            
            time.sleep(job)            
            self.log("finished sleeping")
            # when the job is done, you signal the queue - refer to
            # the Queue module documentation
            self.queue.task_done()


def main(number_of_jobs=10, number_of_workers=3):
    # create the queue where you will put the jobs
    queue = Queue()

    # create the pool of workers (notice that you pass them the queue
    # upon construction). 
    for _ in range(number_of_workers):
        worker = Worker(queue)
        # you "daemonize" a thread to ensure that the threads will
        # close when the main program finishes
        worker.daemon = True        
        worker.start()

    # now it is time to add the jobs to the queue
    for _ in range(number_of_jobs):
        # a random duration between 2 and 5 seconds
        duration = random.randint(2,5) 
        queue.put(duration)

    # now wait for all workers to finish - JOIN THE QUEUE
    queue.join()


if __name__ == "__main__":
    import sys
    if len(sys.argv) == 3:
        nj = int(sys.argv[1])
        nw = int(sys.argv[2])
    else:
        nj = 10
        nw = 3

    # call main             
    main(nj, nw)
样本输出:

computer$ python example.py
25-Feb-2012 21:21:25.924856 UTC Thread-1: sleeping for 2 seconds
25-Feb-2012 21:21:25.925439 UTC Thread-2: sleeping for 3 seconds
25-Feb-2012 21:21:25.925523 UTC Thread-3: sleeping for 5 seconds
25-Feb-2012 21:21:27.925513 UTC Thread-1: finished sleeping
25-Feb-2012 21:21:27.925696 UTC Thread-1: sleeping for 5 seconds
25-Feb-2012 21:21:28.925561 UTC Thread-2: finished sleeping
25-Feb-2012 21:21:28.925742 UTC Thread-2: sleeping for 5 seconds
25-Feb-2012 21:21:30.925547 UTC Thread-3: finished sleeping
25-Feb-2012 21:21:30.925728 UTC Thread-3: sleeping for 5 seconds
25-Feb-2012 21:21:32.925781 UTC Thread-1: finished sleeping
25-Feb-2012 21:21:32.925963 UTC Thread-1: sleeping for 5 seconds
25-Feb-2012 21:21:33.925822 UTC Thread-2: finished sleeping
25-Feb-2012 21:21:33.926003 UTC Thread-2: sleeping for 2 seconds
25-Feb-2012 21:21:35.925833 UTC Thread-3: finished sleeping
25-Feb-2012 21:21:35.926013 UTC Thread-3: sleeping for 3 seconds
25-Feb-2012 21:21:35.926244 UTC Thread-2: finished sleeping
25-Feb-2012 21:21:35.926420 UTC Thread-2: sleeping for 5 seconds
25-Feb-2012 21:21:37.926035 UTC Thread-1: finished sleeping
25-Feb-2012 21:21:38.926158 UTC Thread-3: finished sleeping
25-Feb-2012 21:21:40.926697 UTC Thread-2: finished sleeping
computer$ 

……哈哈,现在我羞愧地把头挂在角落里。谢谢你,伙计。祝你玩得开心!如果你把输出保存到一个文件中(例如,
python3 test.py>out.txt
,或者把它传送到
less
),然后你会在你的终端上看到异常。谢谢!我会记住这一点。顺便说一句,你一定有一台非常棒的机器。我以为我的机器速度很快,但你的机器却失败了。嗯。我使用的是3.3 GHz i5。奇怪为什么它对我来说速度这么慢。我每台机器大约有4秒。@PFranchise:任务是IO限制的。你可能在Windows上,是吧如果您将输出重定向到一个文件(
python-your_script.py>out.txt
),它应该会更快()
computer$ python example.py
25-Feb-2012 21:21:25.924856 UTC Thread-1: sleeping for 2 seconds
25-Feb-2012 21:21:25.925439 UTC Thread-2: sleeping for 3 seconds
25-Feb-2012 21:21:25.925523 UTC Thread-3: sleeping for 5 seconds
25-Feb-2012 21:21:27.925513 UTC Thread-1: finished sleeping
25-Feb-2012 21:21:27.925696 UTC Thread-1: sleeping for 5 seconds
25-Feb-2012 21:21:28.925561 UTC Thread-2: finished sleeping
25-Feb-2012 21:21:28.925742 UTC Thread-2: sleeping for 5 seconds
25-Feb-2012 21:21:30.925547 UTC Thread-3: finished sleeping
25-Feb-2012 21:21:30.925728 UTC Thread-3: sleeping for 5 seconds
25-Feb-2012 21:21:32.925781 UTC Thread-1: finished sleeping
25-Feb-2012 21:21:32.925963 UTC Thread-1: sleeping for 5 seconds
25-Feb-2012 21:21:33.925822 UTC Thread-2: finished sleeping
25-Feb-2012 21:21:33.926003 UTC Thread-2: sleeping for 2 seconds
25-Feb-2012 21:21:35.925833 UTC Thread-3: finished sleeping
25-Feb-2012 21:21:35.926013 UTC Thread-3: sleeping for 3 seconds
25-Feb-2012 21:21:35.926244 UTC Thread-2: finished sleeping
25-Feb-2012 21:21:35.926420 UTC Thread-2: sleeping for 5 seconds
25-Feb-2012 21:21:37.926035 UTC Thread-1: finished sleeping
25-Feb-2012 21:21:38.926158 UTC Thread-3: finished sleeping
25-Feb-2012 21:21:40.926697 UTC Thread-2: finished sleeping
computer$