Get the App
SLTechnology News&Howtos  ›  Servers  › 

Python Thread and process Learning Notes

Shulou Source: shulou.com Published: 2022-06-03 05:41:46 09月21日 Update

Multithreaded vs multiprocess program: a pile of code is stored in a document process in the form of text: a state in which the program runs includes address space, memory, data stack, etc. Each process completes an independent running environment by itself, and multi-process sharing data is an independent running segment of a problem thread and a process. A process can have multiple threads lightweight a process sharing data and context among multiple threads of a process sharing mutual exclusion global interpretation lock (GIL) the execution of python code is controlled by the python virtual machine in the main loop there is a control thread in the use of executing threading directly using threading.Thread to generate Thread examples t = threading.Thread (target=xxx, args= (xxx) ) t.start (): start multithreaded t.join (): wait for the completion of multithreaded execution case 01:

Import threading

Import time

Def loop01 (in01):

Print ("start loop01 at: {0}" .format (time.ctime ()

Print ("loop01 parameter: {0}" .format (in01))

Time.sleep (4)

Print ("end loop01 at: {0}" .format (time.ctime ()

Def loop02 (in01, in02):

Print ('start loop02 at: {0}' .format (time.ctime ()

Print ("loop02 parameter, first parameter {0}, second parameter {1}" .format (in01,in02))

Time.sleep (2)

Print ("end loop02 at: {0}" .format (time.ctime ()

If name = 'main':

Print ("Starting at: {0}" .format (time.ctime ()

T1 = threading.Thread (target=loop01,args= ("loop01 parameter",))

T2 = threading.Thread (target=loop02,args= ("loop02 parameter 1", "loop02 parameter 2"))

T1.start ()

T2.start ()

T1.join ()

T2.join ()

Print ("All done at {0}" .format (time.ctime ()

While True:

Time.sleep (10)

Tags: Process thread parameter run data code multiple environment program problem control independent up and down context global memory address form text document Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Docker Shulou Tech Info Apple Huawei