python threads and processes Learning Notes

Multithreaded vs multiprocess

  • Program: a bunch of code into a document as text
  • Process: a state program running
    • Contains the address space, memory, and other data stack
    • Each process is completed by its own independent operating environment, multiple processes to share data is a problem
  • Thread
    • Stand-alone segment of a process, a process can have multiple threads
    • Lightweight process
    • Between multiple threads of a process share data and context operating environment
    • Shared Exclusive issue
  • Global Interpreter Lock (GIL)
    • Python code execution is controlled by a virtual machine python
    • There is a thread of control executed in the main loop

threading of use

  • Thread exemplary generated directly threading.Thread
    1. t = threading.Thread(target=xxx, args=(xxx,))
    2. t.start (): Start multithreading
    3. t.join (): <font color = red> multithreaded execution waiting completion </ font>
  • Case 01:

import threading
import time

def loop01(in01):
print("start loop01 at : {0}".format(time.ctime()))
print("loop01参数:{0}".format(in01))
time.sleep(4)
print("end loop01 at : {0}".format(time.ctime()))

loop02 DEF (IN01, IN02):
Print ( 'Start loop02 AT: {0}'. the format (time.ctime ()))
Print ( "loop02 parameter, the first parameter {0}, {1} the second parameter ".format (IN01, IN02))
the time.sleep (2)
Print (" End loop02 AT:. {0} "the format (time.ctime ()))

if name == 'main':
print("Starting at : {0}".format(time.ctime()))
t1 = threading.Thread(target=loop01,args=("loop01参数",))
t2 = threading.Thread(target=loop02,args=("loop02参数1","loop02参数2"))
t1.start()
t2.start()
t1.join()
t2.join()
print("All done at {0}".format(time.ctime()))
while True:
time.sleep(10)

Guess you like

Origin blog.51cto.com/11389430/2460258