Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use Gevent in Python

Shulou Source: shulou.com Published: 2022-05-31 10:47:53 10月03日 Update

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.

Tags: Module knowledge industry process different practical operating system main program task content can pass feast instance practicality actual object form article method more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Docker vpn Shulou Technology Shulou Tech Info