coroutine
The Post Created(Updated) On 08/8/2022,Please note the timeliness of the article!
I’ve been coming across the concept of concurrent threads lately, and every time I come across it, I’m confused. Just try to understand this into the thread outside of the strange guy, and finally look at it is not too difficult, simply said is a micro-thread.
Description
- Concurrent threads, also known as micro-threads, slim threads, also known as user-level threads, on the basis of not opening threads to complete multiple tasks, that is, in the case of a single thread to complete multiple tasks, multiple tasks in a certain order of alternate execution.
Implementation method
- Individuals are currently the main technical language is Python, so the following are written based on Python.
# The code implementation of the concurrent yield
- Commonly understood as long as you see only one yield keyword inside the def means that it is a concurrent process.
import time
def work1():
while True:
print("----work1---")
yield
time.sleep(0.5)
def work2():
while True:
print("----work2---")
yield
time.sleep(0.5)
def main():
w1 = work1()
w2 = work2()
while True:
next(w1)
next(w2)
if __name__ == "__main__":
main()
greenlet implementation
- Install
pip install greenlet
- implement
import time
import greenlet
# task1
def work1():
for i in range(5):
print("work1...")
time.sleep(0.2)
# Switch to work2 to execute the corresponding task
g2.switch()
# Task 2
def work2():
for i in range(5):
print("work2...")
time.sleep(0.2)
# Switch to the first worker to execute the corresponding task
g1.switch()
if __name__ == '__main__':
# Create a concurrent process to specify the corresponding task
g1 = greenlet.greenlet(work1)
g2 = greenlet.greenlet(work2)
# Switch to the first worker to perform the corresponding task
g1.switch()
gevent
- Install
pip install gevent
- Implement
import gevent
import time
from gevent import monkey
# Patch to make the gevent framework recognize time-consuming operations, e.g. time.sleep, network request delay
monkey.patch_all()
# Task 1
def work1(num):
for i in range(num):
print("work1....")
time.sleep(0.2)
# gevent.sleep(0.2)
# Task 1
def work2(num):
for i in range(num):
print("work2....")
time.sleep(0.2)
# gevent.sleep(0.2)
if __name__ == '__main__':
# Create a concurrent process to specify the corresponding task
g1 = gevent.spawn(work1, 3)
g2 = gevent.spawn(work2, 3)
# The main thread waits for the concurrent thread to finish executing before exiting the program
g1.join()
g2.join()
The relationship between processes, threads, and concurrent threads
- A process has at least one thread, and a process can have multiple threads inside it
- A thread can have multiple concurrent threads inside it
Comparison of processes, threads, and concurrent threads
- process is a unit of resource allocation
- threads are the unit of OS scheduling
- process switching requires the most resources and is very inefficient
- thread switching requires average resources and has average efficiency (without considering GIL of course)
- the co-process switch task resources are small, high efficiency
- multi-process, multi-thread may be parallel depending on the number of cpu cores, but the concurrent thread is in a thread, so it is concurrent
Summary
- Processes, threads, and concurrent threads are all capable of multi-tasking, so you can choose to use them according to your actual development needs
- Since threads and concurrent threads require few resources, the chances of using threads and concurrent threads are the highest
- Opening a concurrent process requires the least resources
Reference
Note that section 6.3 of the reference, the personal implementation results are not quite consistent with the reference article.
Translated with www.DeepL.com/Translator (free version)
Copyright
Unless otherwise noted, all work on this blog is licensed under a Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0) License. Reprinted with permission from -https://blog.emperinter.info/2022/08/08/coroutine