How to Choose a Suitable R&d Model

Python multitasking programming Introduction:

Python multitasking programming.The purpose of obtaining the process number: In order to verify the relationship between the main process and the child process, it is known which main process the child thread was created by.

Python multitasking programming.The purpose of getting the process number:


In order to verify the relationship between the main process and the child process, it is known which main process the child thread was created by.

Python gets the process number id:

#Get the number of the current process
os.getpid ( ) _

#Get the ID of the parent process
os.getppid ( ) _

Implementation steps:

1. Import the os package
import os

2. Get the process ID, parent process ID
os.getpid ( ) _
os.getppid ( ) _

3. Get the process name
print ( multiprocessing.current_process ( ) ) _

Code:

import multiprocessing
import time

#import os package
import os


def dance ( ):
#Get the process number of dance
print ( 'dance: ' , os . getpid ())
#Get the number of the dance parent process
print ( "dance parent process:" , os . getppid ())
#Get the current process view is executed by that process
print ( 'dance: ' , multiprocessing . current_process ())
for i in range ( 5 ):
print ( "dance" )
time.sleep ( 2 ) _ _

def sing ( ):
#Get the sing process number
print ( 'sing: ' , os . getpid ())
#Get the number of the sing parent process
print ( "sing parent process:" , os.getppid ( ) )
#Get the current process view is executed by that process
print ( 'sing: ' , multiprocessing . current_process ())
for i in range ( 5 ):
print ( "sing" )
time.sleep ( 2 ) _ _


if __name__ == '__main__' :

#Get the current process (main process) number
print ( 'main: ' , os . getpid ())

#Get the current process view is executed by that process
print ( 'main: ' , multiprocessing . current_process ())

dance_process = multiprocessing.Process(target=dance,name='dance_process')
print(dance_process)
sing_process = multiprocessing .Process ( target = sing , name = ' sing_process ' ) _
print ( sing_process )

dance_process.start ( ) _ _
sing_process.start ( ) _ _

Python multitasking programming.By running the above code, we can find:
Main's process number = sing's parent process number = dance's parent process number
this way, we conclude that dance_process and sing_process are subprocesses established by main.

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00