python process thread simply understood

Simple to understand

1, thread: the smallest unit of execution; process: the smallest resource unit

2, a program has at least one process, a process has at least one thread (the thread can be understood as a container thread)

3, the process has a separate memory unit in the implementation process, and multiple threads share memory

4, each thread has a separate entrance program, sequence program, and an outlet executed sequentially , but the thread can not be independently performed, a plurality of threads must be provided by the application program execution control

5, the process is a separate unit of the system resource allocation and scheduling

Multi-threaded code is On

import threading
import time

def sing():
    print("begin to sing            %s" % time.ctime())
    time.sleep(3)
    print("stop to sing             %s" % time.ctime())
    
def jump():
    print("begin to jump            %s" % time.ctime())
    time.sleep(5)
    print("stop to jump             %s" % time.ctime())
    
def rap():
    print("begin to rap             %s" % time.ctime())
    time.sleep(7)
    print("stop to rap              %s" % time.ctime())

def play_basketball():
    print("begin to play_basketball  %s" % time.ctime())
    time.sleep(9)
    print("stop to play_basketball   %s" % time.ctime())
    
    
if __name__ == '__main__':
    #开启sing的线程
    t1 = threading.Thread(target = sing)
    t1.start()
    #开启jump的线程
    t2 = threading.Thread(target = jump)
    t2.start() 
    #开启rap的线程
    t3 = threading.Thread(target = rap)
    t3.start()
    #开启篮球的线程
    t4 = threading.Thread(target = play_basketball)
    t4.start() 

In this way, it can open a thread like cxk the same can be performed simultaneously sing, dance, rap, basketball.

This is a program that is equivalent to a process, and dancing and singing rap basketball like four threads simultaneously executed, their interval time interval just two seconds is not 3579 Miao

Guess you like

Origin www.cnblogs.com/hyxk/p/11279104.html
Recommended