5.1.2 Network Programming Advanced---Two ways to turn on sub-processes

Source: Internet
Author: User

When the primary process opens a child process, the master process executes in parallel with the child process. When the main process does not immediately end the process, but wait until the end of the child process to end, so as to clean up the zombie process (to the child process corpse).

The first way:

From multiprocessing import Process
Import time


def task (name):
Print ('%s is running '% name)
Time.sleep (3)
Print ('%s is do '% name)


if __name__ = = ' __main__ ':
p = Process (Target=task, kwargs={' name ': ' subprocess 1 '})
# p = Process (Target=task, args= (' sub-process 1 ',))
P.start () # simply sends a signal to the operating system, and then continues to execute without waiting for the child process to complete. However, you can end the main process when the child process is finished.

Print (' master ')

#会执行子进程执行结束后 to end their process.

# Output Results:
# Master
# Child Process 1 is running
# child Process 1 is done


The second way: the way of inheritance
From multiprocessing import Process
Import time


Class Myprocess (process): # Inherit the Process class
def __init__ (self, name):
Super (). __INIT__ ()
Self.name = Name

def run (self): # must override the Run method
Print (' subprocess starting%s '% self.name)
Time.sleep (3)
Print (' End subprocess%s '% self.name)


if __name__ = = ' __main__ ':
p = myprocess (' xxx ')
P.start () # Start automatically binds to the Run method
Print (' main thread ')

# Output Results:
# main Thread
# subprocess Starting XXX
# End subprocess XXX

5.1.2 Network Programming Advanced---Two ways to turn on sub-processes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.