How to use Gevent in Python
This article mainly shows you "how to use Gevent in Python", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "how to use Gevent in Python" this article.
1. Concurrent synchronous or asynchronous programming can be easily realized through gevent. The main mode used in gevent is Greenlet, which is a lightweight protocol that accesses Python in the form of C extension modules.
2. Greenlet all runs in the process of the main program operating system, but they are cooperative scheduling.
Example
From gevent import monkey; # in order to identify the iomonkey.patch_all () # of the time module, it must be placed in front of the patched, such as import gevent# pip install geventfrom time import time,sleep def gf (name): print (f'{name}: I want to play Masters!) before the time,socket module. # gevent.sleep (2) sleep (2) print (f'{name}: I want a big meal!') Def bf (name): print (f'{name}: play together!') # gevent.sleep (2) sleep (2) print (f'{name}: go and eat!) If _ _ name__ = = "_ _ main__": start = time () # create the cooperative program object G1 = gevent.spawn (gf,' Diao Chan') G2 = gevent.spawn (bf,' Lv Bu') # start the task g1.join () g2.join () end = time () print (end-start) above are all the contents of this article "how to use Gevent in Python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!