mirror of
https://github.com/Brandon-Rozek/website.git
synced 2024-11-09 18:50:34 -05:00
New Post
This commit is contained in:
parent
e7a79e5978
commit
57b6850666
1 changed files with 33 additions and 0 deletions
33
content/blog/gevent.md
Normal file
33
content/blog/gevent.md
Normal file
|
@ -0,0 +1,33 @@
|
|||
---
|
||||
title: "Gevent"
|
||||
date: 2020-04-09T17:22:52-04:00
|
||||
draft: false
|
||||
tags: ["python", "concurrency"]
|
||||
---
|
||||
|
||||
In my last post I spoke about [concurrency with asyncio](https://brandonrozek.com/blog/pyasyncio/). Now what if you don't want to concern yourself with async/await practices and just want to write synchronous code that executes I/O asynchronously? That's where the library [gevent](http://www.gevent.org/) comes in. It does this by modifying Python's standard library during runtime to call it's own asynchronous versions.
|
||||
|
||||
Last post code's example written in `gevent`.
|
||||
|
||||
```python
|
||||
# The first two lines must be called before
|
||||
# any other modules are loaded
|
||||
import gevent
|
||||
from gevent import monkey; monkey.patch_all()
|
||||
|
||||
import time
|
||||
|
||||
def think(duration):
|
||||
print("Starting to think for " + str(duration) + " seconds...")
|
||||
time.sleep(duration)
|
||||
print("Finished thinking for " + str(duration) + " seconds...")
|
||||
|
||||
gevent.wait([
|
||||
gevent.spawn(think, 5),
|
||||
gevent.spawn(think, 2)
|
||||
])
|
||||
```
|
||||
|
||||
Notice that the function `think` is written the same as the synchronous version.
|
||||
|
||||
`gevent` is written on top of C libraries `libev` or `libuv` . This combined with the monkey patching can make `gevent` based applications hard to debug if something goes wrong. Otherwise it's a great tool to quickly take advantage of concurrency.
|
Loading…
Reference in a new issue