Multi-process and join usage

# # **************************** 
# # Create a multi-process mode
# Import OS
# Import from multiprocessing process
#
# Task DEF ():
# Print ( 'the this Sub process IS')
# Print (process ID f'sub os.getpid {()} ')
# Print (f'my ID os.getpid {()} {os.getppid IS PPID ()} ')
#
#
# Print (os.getpid (), __ name__)
#
# IF the __name__ ==' __ main__ ':
# # Note open Code __main__ process must be placed below
## instances of a process object, let him start a process
# = the p-process (target = Task)
# p.start ()
# Print ( 'the this iS parent process')
# Print ( Process IS f'parent: os.getpid {()} ')
# Print (' over ')
#
# """
# 4280 __main__
# this is parent process
# parent process is:4280
# over
# 5576 __mp_main__
# this is sub process
# sub process id 5576
# my id 5576 ppid is 4280
# """
# # ************************************************
# from multiprocessing import Process
# import os
#
# class Downloarder(Process):
# # def __init__(self,url,size,name):
# # super().__init()
# # self.url = url
# # self.size = size
# # self.name = name
#
#
# def run(self):
# print(os.getpid())
# pass
#
#
# if __name__ == '__main__':
# m = Downloarder()
# m.start()
# m.run()
# print('parent over',os.getpid())
#
# """
# 1648
# parent over 1648
# 5684
#
# """
# ************************************************
# from multiprocessing import Process
# import os,time
#
# a = 257
#
# def task():
# global a
# print("2",a,id(a))
# a = 200
#
# if __name__ == '__main__':
# p = Process(target=task)
#
# p.start()
#
# time.sleep(5)
# print(a)
#
# """
# 2 257 2903098186800
# 257
# """
# ************************************************
# from multiprocessing import Process
# import os,time
#
# a = 257
#
# def task():
# while True:
# print("子")
#
# if __name__ == '__main__':
#
#
# p = Process(target=task)
# p.start()
# while True:
# print('主')
#
# time.sleep(4)
# print(a)
#
# """
# 主
# 子
# 子
# 子
# 子
# 子
# ......(子)
#
# """
**************************** #
# 5. join function

# Import from multiprocessing Process
# Import OS, Time
#
# Task DEF ():
# for I in Range (100):
# Print ( 'sub RUN')
#
# IF the __name__ == '__main__':
# P process = (target = Task)
# p.start () to send commands to the operating system #
# p.join () # get the main process is finished and then wait for the child to continue
#
# Print ( 'over')
#
#
# "" "
# there p.join ()
# child run
# ... (97) detected ...
# run sub-
unit # run
# over
#
#
# no p.join ()
# over
# child run
# run sub-
unit # run
# ....... (97)
#
# "" "
# **************************** ***
# Import from multiprocessing Process
# Import OS, Time
#
# DEF Task1 (Time):
# for I in Range (1000):
# Print (F '{name} RUN')
#
# DEF Task2 (Time):
# I in Range for (1000):
# Print (F '{name} RUN')
#
# IF the __name__ == '__main__':
# Process P1 = (target = Task1, args = ( "P1",))
# P1. Start ()
# P2 = process (target = task2, args = ( "P2",))
# p2.start ()
#
# p2.join () # wait for the child to get the main process is finished and then execute
# p1.join ( )
#
#
# # to achieve the effect p1, p2 two programs are executed concurrently, and over must be printed only after completion of all program execution
Print # ( 'over')
#
#
# **************************************** ********

# using the Join
# Import OS, Time
# Import from multiprocessing Process
#
# DEF Task (name):
# for I in Range (. 4):
# Print (F '{name} RUN')
#
# IF the __name__ == '__main__': parameters # args is a sub-process transfer must be ancestral
#
#
# PS = []
# for I in Range (. 4):
# P = process (target = Task, args = (I,))
# p.start ()
# ps.append (P)
#
# # join one by one the following
# for I in PS:
# i.join ()
#
# Print ( 'over')
#
#
# "" "
# 0 run
# 0 RUN
# 0 RUN
# 0 RUN
#. 1 RUN
#. 1 RUN
#. 1 RUN
#. 1 RUN
# 2 RUN
# 2 RUN
# 2 RUN
# 2 RUN
#. 3 RUN
#. 3 RUN
#. 3 RUN
#. 3 RUN
# over
# "" "

# ****************************
Process multiprocessing Import from
Import OS, Time

DEF Task ():
the while True:
Print ( 'sub RUN', os.getpid ())
# os.getpid () itself ID os.getppid () of the parent class ID


IF = the __name__ = '__main__':
the p-process = (target = Task, args = "old driver process")
p.start ()
p.join ()
Print (p.name)

# # daemon p.daemon
P.join # ()
# Print (p.exitcode) # get the process exit code --- use: for example, set 0: correct 1: Account errors, 2: wrong password, 3, wrong address. . .
# Print (p.is_alive ()) # view the process is alive
# print ( 'zi', p.pid ) # get the process ID
# p.terminate () # to terminate the process and start different is not terminated immediately, because operating system there are a lot of things to do

IF __name__ == '__main__':
"" "
1. create a server socket object, bind IP, port, start listening for the main process

2. connect the main loop process

3. communication child process to the cycle a client will open a sub-process
. "" "

Guess you like

Origin www.cnblogs.com/zrx19960128/p/11123347.html