coroutine

#coroutine
#~ Also known as micro-threads
'''
It is a smaller execution unit than a thread because it has its own CPU context. So as long as the right time, we can switch a coroutine to another coroutine
As long as the process saves or restores the CPU context, then the program can run
Popular understanding: a function in a thread can save some temporary variables and other information of the current function anywhere, and then switch to another function for execution

yield--indicates a pause
'''
import time
def A():
    while True:
        print("~~~A~~~")
        yield
        time.sleep(0.5)
def B(a):
    while True:
        print('---B---')
        a.__next__()
        time.sleep(0.5)

if __name__ == '__main__':
    a = A()
    B(a)
    # A(b)

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324652675&siteId=291194637