Multithreaded Code explanation

Source: Internet
Author: User

Threading Module Introduction

The Multiprocess module completely imitates the interface of the threading module, which has a great similarity in the use level, so it is no longer described in detail

Website Link: https://docs.python.org/3/library/threading.html?highlight=threading#

Two ways to turn on multithreading

Method 1: Common

From threadingImportThreadDefTask ():Print‘is running‘)If__name__==‘__main__‘: T=thread (target=Task,) T.start ()#The overhead of opening a thread is small, the speed of the thread is open in the process, the thread is opened based on the current process spacePrint‘Main‘)‘‘‘Turn on the print result of a thread: print is running main‘‘‘From multiprocessingImportProcessDefTask ():print ( is Ruuing '  ' if __name__== ' 

Method 2: Not commonly used, some people use it.

From threadingImportThreadClassdefprint ( is Running ' ) if __name__== ' __main__ '  ' 
The difference between opening multiple threads under one process and opening multiple sub-processes under one process
 fromThreadingImportThreadclassMythread (Thread):defRun (self):Print('is running')if __name__=='__main__': T=Mythread () T.start ( )Print('Master')" "is running Master" " fromThreadingImportThread fromMultiprocessingImportProcessImportOSdeftask ():Print('%s is running'%os.getpid ())if __name__=='__main__': T1=thread (target=task,) T2= Thread (target=task,) T1.start () T2.start ( )Print('Master', Os.getpid ())" "#都是在同一个进程中1884 is running1884 is running main 1884" " fromThreadingImportThread fromMultiprocessingImportProcessImportOSdeftask ():Print('%s is running'%os.getpid ())if __name__=='__main__': P1=process (target=task,) P2= Process (target=task,) P1.start () P2.start ( )Print('Master', Os.getpid ())" "#主进程id和两个子进程id主 78166452 is running8436 running" "
PID
 fromThreadingImportThread fromMultiprocessingImportPROCESSN=100defWork ():GlobalN N=0if __name__=='__main__': P=process (target=Work ,) P.start () P.join ( )Print('Master', N)" "The main 100 found n is not modified because the main process is the program to run this file, the child process is P, only the process and child process space completely independent creation of the child process p, the child process copies the state of the parent process, including n=100, and then global n=0 The global variable of the child process is modified, and the printed n is the global variable of the parent process, so there is no change" " fromThreadingImportThread fromMultiprocessingImportPROCESSN=100defWork ():GlobalN N=0if __name__=='__main__': T=thread (target=Work ,) T.start ()Print('Master', N)" "Master 0 Discovery changes the space sharing between threads in the same process" "
name Space

Multithreaded Code explanation

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.