Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将线程休眠在if语句检查、Python池上_Python_Sleep_Pool - Fatal编程技术网

将线程休眠在if语句检查、Python池上

将线程休眠在if语句检查、Python池上,python,sleep,pool,Python,Sleep,Pool,我有一个代码,读取一个非常大的文本文件,并处理池中的每一行 对于elif我需要将整个过程休眠120秒,换句话说,我希望创建的所有其他池都暂停。但120秒后,所有池应恢复工作 代码的功能与此类似: from multiprocessing import Pool import sys sys.tracebacklimit = 0 def req(line): if "@" not in line: # (some function for processing her

我有一个代码,读取一个非常大的文本文件,并处理池中的每一行

对于elif我需要将整个过程休眠120秒,换句话说,我希望创建的所有其他池都暂停。但120秒后,所有池应恢复工作

代码的功能与此类似:

from multiprocessing import Pool
import sys

sys.tracebacklimit = 0

def req(line):

    if "@" not in line:
        # (some function for processing here)
        return line
    elif "somestring" in line:
        #HERE I NEED TO SLEEP ALL POOLS
    else:
        # (some function for processing)
        return line


if __name__ == "__main__":
    pool = Pool(20)
    with open("list.txt") as source_file:
        # chunk the work into batches of 20 lines at a time
        pool.map(req, source_file, 35)
正如所说,您应该使用
事件
对象,如下所示:

from multiprocessing import Pool
import sys
from threading import Event, Timer

sys.tracebacklimit = 0

# Setup clojure environment
def reqgen():
    ev_stop = Event()

    def req(line):

        # Wait at the start
        if ev_stop.is_set():
            ev_stop.wait()

        if "somestring" in line:
            #HERE I NEED TO SLEEP ALL POOLS

            # Clear the internal flag, make all workers await
            ev_stop.clear()

            # An alarm to reset the internal flag,
            # which will make all workers get back to work
            Timer(120, lambda: ev_stop.set()).start()

            # Regular work
            return req(line)

        else:
            # (some function for processing)
            return line

    return req

if __name__ == "__main__":
    pool = Pool(20)
    with open("list.txt") as source_file:
        # chunk the work into batches of 20 lines at a time
        pool.map(reqgen(), source_file, 35)

你的压痕到处都是。请把它修好。Python对空间非常敏感,您显示的内容无法运行。您正在尝试暂停所有工作线程,而不是池。只有一个池。
多处理。池
没有暂停或休眠池的方法。如果您不关心Windows,您可以获取池中的所有
进程,获取每个进程的
\u popen
对象或PID,并向它们发送一个
SIGSTOP
信号。但这可能不是最好的方法。一个可能更好的解决方案是使用同步对象,如
事件
,每个任务
在开始时等待
(或者,如果他们正在做大量工作,可能每次都通过一个循环)。然后,要暂停所有操作,您只需清除事件,并设置警报以在120秒内设置事件。(如果您希望能够执行递归暂停,以检测是否每个人都已到达事件和阻止等,您将需要比
事件
更复杂的操作)您在这里尝试实现什么?为什么要暂停整个游泳池?