Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.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/6/multithreading/4.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
Python在完成线程列表的执行后创建信号量或检查_Python_Multithreading_Python 3.x - Fatal编程技术网

Python在完成线程列表的执行后创建信号量或检查

Python在完成线程列表的执行后创建信号量或检查,python,multithreading,python-3.x,Python,Multithreading,Python 3.x,我运行的脚本需要运行许多外部命令。我有几个命令块需要一起并行运行 因此,我需要控制这个线程池是否已完成,以启动下一个线程池 要运行的命令: import pathlib def thread_command(path_files, command): _path_files= pathlib.Path(path_files) if pathlib.Path(path_files).exists(): for file in _path_files.glob('**/*.e

我运行的脚本需要运行许多外部命令。我有几个命令块需要一起并行运行

因此,我需要控制这个线程池是否已完成,以启动下一个线程池

要运行的命令:

import pathlib
def thread_command(path_files, command):
    _path_files= pathlib.Path(path_files)
    if pathlib.Path(path_files).exists():
    for file in _path_files.glob('**/*.ext'):

        ext_file = Ext(file, path_files, tmp_rules)
        if threads == None:
            threads.insert(0, threading.Thread(target=command))
        else:
            threads.append(threading.Thread(target=command))
    return threads

#Threads running
command1 = ext_file.command1
command2= ext_file.command2
threads1 = thread_command(pathToFiles, command1)
for thread in threads:
   thread.daemon = True
   thread.start()
 do_other_things()
 threads2 = thread_command(pathToFiles, command2)
 for thread in threads2:
    thread.daemon = True
    thread.start()
我需要找到一种方法或过程来控制第一个线程列表何时完成,以继续执行程序。 总之,我希望运行整个列表线程,并等待所有线程在那里运行完成,然后启动第二个线程池


提前感谢您的帮助。

不要使用“守护进程”线程,而是等待它们重新加入主线程,例如:

commands = [ext_file.command1, ext_file.command2]  # etc.
for command in commands:  # loop through commands in succession
    # get the thread list and start it immediately
    threads_list = [t for t in thread_command(pathToFiles, command) if t.start() is None]
    for t in threads_list:  # loop through all started threads and...
        t.join()  # ... wait for them to join back

但是您的
thread\u command()
函数本身没有什么意义-对于初学者来说,当您尝试插入/附加
threads
列表时,会出现错误,因为它没有定义(至少不在函数上下文中)。第二,为什么要对每个命令反复检查文件列表,除非您希望文件列表发生更改?

是否有理由仅仅使用join()不起作用?连接将等待线程完成。因此,遍历线程列表并调用join()?
如果线程==None:threads.insert(…)
将导致错误,如果
threads
为None,它将没有
insert
方法…为什么还要涉及线程?如果您正在使用子流程运行外部命令,则可以在调用之前简单地触发一组命令。好的,谢谢,我会检查一下。是的,我希望文件列表会改变。然而,我认为这就像我在手动完成整个过程一样。你说得对。我将重新设计代码的逻辑。