Python crawler "2nd article"

Source: Internet
Author: User

One, multi-process

1.fork method (OS module, for LUnix systems)

Fork method: Call 1 times and return 2 times. Cause: The operating system replicates a process (child process) through the current process (the parent process), the two processes are almost identical, the fork method is returned in the parent process, the child process, the child process returns the value 0, and the parent process returns the ID of the child process.

Normal method: Call 1 times, return 1 times

ImportOSif __name__=='__main__':    Print 'Current Process (%s) start ....'% (Os.getpid ())#getpid () user gets the current process IDPID =os.fork ()ifPID <0:Print 'Error in Fork'    elifPID = =0:Print 'I AM Child process (%s)'  andMy parent process is(%s)', (Os.getpid (), Os.getppid ())    Else:        Print 'I (%s) created a child process (%s).', (Os.getpid (), PID) operation results are as follows: Current Process (3052) Start .... I (3052) created a child process (3053). I am Child process (3053) andMy parent process is(3052)

2.multiprocessing (cross-platform)

ImportOS#importing the Process class from the multiprocessing module fromMultiprocessingImportProcessdefRun_proc (name):Print 'Child Process%s (%s) Running ...'%(Name,os.getpid ())if __name__=='__main__':    Print 'Parent process%s.'%os.getpid () forIinchRange (5): P= Process (target = Run_proc,args =(str (i),))Print 'Process would start'        #used to start a processP.start ()#used to implement inter-process synchronizationP.join ()Print 'Process End'The execution results are as follows: Parent process2392. Process would start. Process would start. Process would start. Process would start. Process would start. Child process2 (10748) runing ... Child Process 0 (5324) runing ... Child process1 (3196) runing ... Child process3 (4680) runing ... Child process4 (10696) runing ... Process End

Python crawler "2nd article"

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.