多线程不同的脚本 [英] Multi-threading different scripts

查看:43
本文介绍了多线程不同的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些用 python 编写的脚本.我正在尝试对它们进行多线程处理.

I have aa few scripts written in python. I am trying to multi thread them.

脚本 A 启动时.我希望启动脚本 B、C 和 D.A运行后,我将A2运行.B运行后,我会运行B2,然后运行B3.C 和 D 没有后续脚本.

When Script A starts. I would like scripts B, C, and D to start. After A runs, I would A2 to run. After B runs, I would B2 to run, then B3. C and D have no follow up scripts.

我已经检查过脚本是相互独立的.
我打算使用 "exec" 来启动它们,并希望在 Linux 和 Windows 上使用这个启动器"."

I have checked that the scripts are independent of each other.
I planning on using "exec" to launch them, and would like to use this "launcher" on Linux and Windows."

我有其他多线程脚本,主要做一个有五个线程的过程A.这让我很困惑,因为所有程序都不同,但可以同时启动和运行.

I have other multi thread scripts mainly do a procedure A with five threads. This throwing me because all procedures are different but could start and run at the same time.

推荐答案

好吧,我仍然不确定您的问题到底出在哪里,但我就是这样解决问题的:

Ok I'm still not sure where exactly your problem is, but that's the way I'd solve the problem:

#Main.py
from multiprocessing import Process
import ScriptA
# import all other scripts as well

def handle_script_a(*args):
    print("Call one or several functions from Script A or calculate some stuff beforehand")
    ScriptA.foo(*args)

if __name__ == '__main__':
    p = Process(target=handle_script_a, args=("Either so", ))
    p1 = Process(target=ScriptA.foo, args=("or so", ))
    p.start()
    p1.start()
    p.join()
    p1.join()

# ScriptA.py:
def foo(*args):
    print("Function foo called with args:")
    for arg in args:
        print(arg)

您可以直接调用一个函数,或者如果您想在一个进程中调用多个函数,请为其使用一个小包装器.没有平台相关的代码,没有丑陋的执行程序,您可以以任何您喜欢的方式轻松创建/加入流程.

You can either call a function directly or if you want to call several functions in one process use a small wrapper for it. No platform dependent code, no ugly execs and you can create/join processes easily in whatever way fancies you.

还有一个用于进程间通信的队列的小例子 - 几乎是从 python API 中窃取的,但很好;)

And a small example of a queue for interprocess communication - pretty much stolen from the python API but well ;)

from multiprocessing import Process, Queue

def f(q):
    q.put([42, None, 'hello'])

if __name__ == '__main__':
    q = Queue()
    p = Process(target=f, args=(q,))
    p.start()
    print(q.get())    # prints "[42, None, 'hello']"
    p.join()

创建队列并赋予它一个或多个进程.请注意,get() 会阻塞,如果您想要非阻塞,可以使用 get_nowait() 或将超时指定为第二个参数.如果你想要共享对象会有 multiprocessing.Array 或 multiprocessing.Value,只需阅读文档以获取特定信息 文档链接如果您有更多与 IPC 相关的问题,请创建一个新问题 - 本身就是一个非常大的话题.

Create the queue and give it one or more processes. Note that get() blocks, if you want non blocking you can use get_nowait() or specify a timeout as 2nd argument. If you want shared objects there'd be multiprocessing.Array or multiprocessing.Value, just read the documentation for specific information doc link If you've got more questions relative to IPC create a new question - a extremely large topic in itself.

这篇关于多线程不同的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆