How to use Gevent in Python
This article mainly introduces the relevant knowledge of "how to use Gevent in Python". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to use Gevent in Python" can help you solve the problem.
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 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), that's all for "how to use Gevent in Python". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.