简体   繁体   English

使用 Python 在新进程中执行函数

[英]Executing a function in new process with Python

Is there a way to do this?有没有办法做到这一点? I was thinking maybe use subprocess or multiprocessing but I am not sure how to do this?我在想可能使用 subprocess 或 multiprocessing 但我不知道如何做到这一点?

I don't have any code for an example because it is just a general question.我没有任何示例代码,因为这只是一个一般性问题。

http://docs.python.org/library/subprocess.html http://docs.python.org/library/subprocess.html

EDIT: Probably, I missed, what you want.编辑:可能,我错过了,你想要什么。 Well, It all I can imagine about your question.好吧,关于你的问题,我只能想象。

subprocess.call(["ls","-lAd"]) # executes external program. Like system()
# Capture output. Like popen, as I remember.
subprocess.check_output(["echo", "Hello World!"]) 
os.fork() # Binding to fork()

class MyThread(threading.thread):
    def run():
        print("Hello from thread")

MyThread().start()

yes there is就在这里
python provides 2 different ways of doing this threading and multiprocessing witch one you should use depend on the operation your performing. python 提供了 2 种不同的方法来执行此线程和多处理,您应该使用一种取决于您执行的操作。
the main difference is that Threading executes the function in the same interpreter while multiprocessing starts a new interpreter and runs the function in that interpreter.主要区别在于线程在同一个解释器中执行函数,而多处理启动一个新的解释器并在该解释器中运行该函数。 this means that multiprocessing is genraly used when your performing cpu bound operations like adding a lot of numbers and Thread is used for iobound operations like inputs or waiting for something to happen.这意味着当您执行 cpu 绑定操作(例如添加大量数字)和 Thread 用于 iobound 操作(例如输入或等待某事发生)时,通常会使用多处理。
threading Example:线程示例:

from threading import Thread
import time

def fun(a):
    global myVar
    myVar = "post start" # as you can see myVar is updated and can be read by the main Thread
    time.sleep(1)
    f = input(a)
    print(f"inputed {f}")

myVar = "preThread"
t = Thread(target=fun,
           args=("plz input your message ",))
t.start() # start the thread 

print("this whil run after the thread started", myVar)
t.join() # wait for thread to finisch executing
print("this whil run after the thread ended", myVar)

outputs产出

this whil run after the thread started post start
plz input your message k
inputed k
this whil run after the thread ended post start

if you use the multiprocessing lib it starts a new python interpreter and all values are copied into it, and print and inputs wont work如果您使用多处理库,它会启动一个新的 Python 解释器并将所有值复制到其中,并且打印和输入将不起作用

from multiprocessing import Process
import time

def fun(a):
    global myVar
    myVar = "post start" # as you can see myVar is updated and can be read by the main Thread
    time.sleep(1)
    f = input(a)
    print(f"inputed {f}")

myVar = "preThread"
t = Process(target=fun,
            args=("plz input your message ",))
t.start() # start the thread 

print("this whill run after the thread started", myVar)
t.join() # wait for thread to finisch executing
print("this whill run after the thread ended", myVar)

outputs:输出:

this whill run after the thread started preThread
this whill run after the thread ended preThread

if you want to know more plz read如果你想知道更多请阅读
https://docs.python.org/3/library/threading.html for thread https://docs.python.org/3/library/threading.html线程
https://docs.python.org/3/library/multiprocessing.html for multiprocessing\\ https://docs.python.org/3/library/multiprocessing.html用于多处理\\

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

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