简体   繁体   中英

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?

I don't have any code for an example because it is just a general question.

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.
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.
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

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/multiprocessing.html for multiprocessing\\

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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