简体   繁体   English

用Python生成后入睡

[英]Sleep after spawn in Python

I would like to launch 4 threads to do some work every 1 second in Python. 我想启动4个线程,以便在Python中每1秒执行一些工作。 To make sure the work is done every 1 sec I inserted a sleep after the spawn as shown in the following code snippet. 为了确保工作每1秒钟完成一次,我在生成之后插入了一个睡眠,如以下代码片段所示。 From printing it seems that the number of times do_work was executed was 10 while I was expecting 10*4 --> the number of loop iterations x the number of threads. 从打印看来, do_work执行次数为10,而我期望为10 * 4->循环迭代次数x线程数。 Also, it seems that the first thread starts to do the work after 4 seconds from the time printed in the main loop.(I printed the time also inside the thread). 另外,似乎第一个线程在主循环中打印的时间起4秒后开始工作(我也在线程内部打印了时间)。

    run_pool = GreenPool(4)
    counter = 0
    while counter < 10:
        run_pool.spawn(self.do_work)
        time.sleep(1)
        counter += 1
        print time.time()

First of all, you should read the documentation to get a better idea of how eventlet works and how to use it. 首先,您应该阅读文档以更好地了解eventlet工作方式以及如何使用它。 I myself have never used eventlet , everything that follows I figured out by skimming the docs for a few minutes. 我本人从未使用过eventlet ,通过浏览文档几分钟,我发现了以下所有问题。

it seems that the number of times do_work was executed was 10 while I was expecting 10*4 看来do_work的执行次数是10,而我期望的是10 * 4

GreenPool.spawn() only spawns one thread per call. GreenPool.spawn()每次调用仅产生一个线程。 The size of the pool limits how many threads can be running at once. 池的大小限制了一次可以运行多少个线程。 If all threads in the pool are in use then spawn will block until a thread becomes available. 如果池中的所有线程都在使用中,则spawn将阻塞,直到有一个线程可用为止。

it seems that the first thread starts to do the work after 4 second from the time printed in the main loop 似乎从主循环中打印的时间起第4秒之后,第一个线程开始执行工作

You need to monkey-patch the time module to make it voluntarily yield control while sleeping. 您需要对time模块进行猴子修补,以使其在睡眠时自动屈服控制。 Since you haven't done this, the spawned threads can't do work while the main thread is sleeping. 由于尚未执行此操作,因此在主线程处于休眠状态时,产生的线程无法工作。 As a result, the threads don't run until the 4th iteration of the loop. 结果,线程直到循环的第4次迭代才运行。 At that point, the main thread can't spawn any more threads so it "blocks" and yields control to the worker threads. 那时,主线程无法产生更多线程,因此它“阻塞”并控制工作线程。

Calling eventlet.monkey_patch() before your loop will fix the problem. 在循环之前调用eventlet.monkey_patch()将解决此问题。 Alternatively, use eventlet.sleep() instead of time.sleep() . 或者,使用eventlet.sleep()代替eventlet.sleep() time.sleep()

The parameter to the constructor for GreenPool is the number of worker threads that will be used. GreenPool的构造函数的参数是将使用的工作线程数。 By passing the number 4, you are telling it to call self.do_work by a maximum of four times simultaneously. 通过传递数字4,您告诉它最多同时调用self.do_work四次。 Since you call spawn ten times, you've queued up 10 "jobs", so that's how many times do_work will get called. 由于您调用了十次spawn ,因此您已经排队了10个“职位”,因此do_work将被调用多少次。 The amount of parallelism (4 in this case) doesn't affect the number of times your tasks get execute, but instead limits the amount of threads that may run simultaneously. 并行度(在这种情况下为4)不会影响任务执行的次数,而是会限制可以同时运行的线程数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM