简体   繁体   中英

Starting a process as a subprocess in Python

I am writing a program that uses multiple worker processes (a pre-forking model) with the following code.

from  multiprocessing import Process
for i in range(0,3):
    Process(target=worker, args=(i,)).start()

I use Windows. I notice that they are run as separate processes when I wanted them to start as subprocesses instead. How do I make them subprocesses of the main process?

I am hesitant to use the subprocess module as it seems suited to run external processes (as far as I have used it).


An update : It seems Windows does not launch new processes as sub-processes. Python doesnt support getppid() (get parent's PID) in Windows.

You seem to be confusing terminology here. A subprocess is a separate process. The processes that are created will be children of the main process of your program, and in that sense are subprocesses. If you want threads, then use multithreading instead of multiprocessing , but note that Python won't use multiple cores/CPUs for multiple threads.

I am hesitant to use the subprocess module as it seems suited to run external processes

I'm sorry, I don't understand this remark.

What do you wall subprocess ? To me they are subprocess of your main process. Here my example and returned output.

import time, os
from  multiprocessing import Process

def worker():
    print "I'm process %s, my father is %s" % (os.getpid(), os.getppid())

print "I'm the main process %s" % os.getpid()
for i in range(0,3):
    Process(target=worker).start()

The output is :

I'm the main process 5897
I'm process 5898, my father is 5897
I'm process 5899, my father is 5897
I'm process 5900, my father is 5897

You have 3 subprocesses attached to a main process...

Short answer: http://docs.python.org/library/threading.html

Longer: I don't understand the question, aitchnyu. In the typical Unix model, the only processes a process can start are subprocesses. I have a strong feeling that there's a vocabulary conflict between the two of us I don't know how to unravel. You seem to have something like an "internal process" in mind; what's an example of that, in any language or operating system?

I can attest that Python's subprocess module is widely used.

You write "... multiple working threads ..." Have you read the documentation to which I refer in the first line at the top of this response?

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