SoFunction
Updated on 2024-11-12

A brief look at python co-processing

What is a concurrent program?

Concatenation is python kind of a way to realize multitasking, he is a smaller unit than the thread, occupies a smaller execution unit (resources), why say he is an execution unit, because he comes with a CPU context, so that in the right gr time, you can switch a concatenation to another concatenation, as long as in the process of preservation and restoration of the cpu context, then the program can still be run!

In layman's terms: a function in a thread can save some temporary variables of the current function anywhere, and then switch to another function to run, and the number of times it switches and when it switches back is controllable.

Differences between concatenation and threading

In the implementation of multitasking, the threads will be happy to sub some of their own data, the operating system needs to be restored when switching data, so the thread recovery is more performance-consuming

Underlying principles of concurrent programs (examples)

'''
1, the realization principle of the concurrent program, the underlying realization through yield
'''
def work1(): # Work assignments
  for i in range(10):
    print('work1----{}'.format(i))
    yield i
def work2():
  for i in range(10):
    print('work2----{}'.format(i))
    yield i
def work3():
  for i in range(10):
    print('work3----{}'.format(i))
    yield i
g1 = work1()
g2 = work2()
g3 = work3()
while True: # Loop call generator for task switching
  try:
    print(next(g1))
    print(next(g2))
    print(next(g3))
  except StopIteration :
    pass
    break

Concurrent greenlet

python -m pip install greenlet

typical example

'''
1,Show greentelent ,can't switch automatically need to switch manually
'''
import greenlet
def work1():
  for i in range(10):
     print('work1----{}'.format(i))
     ()  #Switch to g2
def work2():
  for i in range(10):
     print('work2----{}'.format(i))
     () #Switch to g1
g1 = (work1) # Return the concurrent object
g2 = (work2)
() #starter switch

order of execution

gevent

greenlet has already implemented concurrency, but it's still a manual switch. gevent provides an automatic switching function, which is based on the principle that when an IO operation is encountered during execution, the switching will be automatic.

'''
Concurrent gevent IO operations will toggle
'''
import gevent
def work1(): # Work assignments
  for i in range(10):
    print('work1----{}'.format(i))
    (0.5)
def work2():
  for i in range(10):
    print('work2----{}'.format(i))
    (0.5)
g1 = (work1) # Specify the job function
g2 = (work2) # Specify the job function
()  #Wait for the concatenation to complete before moving on
()

This is the whole content of this article.