Python multi-process, process iterative creation, resulting in computer crashes problem solving

Source: Internet
Author: User

ImportOSImportThreadingImportMultiprocessing#worker Functiondefworker (sign, lock): Lock.acquire ()Print(sign, os.getpid ()) lock.release ()if __name__=='__main__':    Print('Main:', Os.getpid ())#multi-threadRecord =[] Lock=Threading. Lock () forIinchRange (5): Thread= Threading. Thread (target=worker,args= ('Thread', Lock)) Thread.Start () record.append (thread) forThreadinchRecord:thread.join ()#multi-processRecord =[] Lock=multiprocessing. Lock () forIinchRange (5): Process= multiprocessing. Process (target=worker,args= ('Process', Lock)) Process.Start () record.append (process) forProcessinchRecord:process.join ()

If you do not write if __name__ = = ' __main__ ': There will be countless processes that eventually occupy all memory reasons:
__name__ is a method that indicates how the current py file is called. If it is equal to "__main__" means to execute directly, if not, it is used to be called by another file, this time if is false, then it will not execute the outermost code.
In this case, when the child process loads the file, it calls the Processfunc function, which executes the code that generates the child process, because he is the outermost code, or global code, which produces multiple processes.

Python multi-process, process iterative creation, resulting in computer crashes problem solving

Related 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.