Skip to content

Instantly share code, notes, and snippets.

@jimjh
Last active November 9, 2019 08:01
Show Gist options
  • Save jimjh/d6abe3b6b0c2a88256d1fc4b3df0da5b to your computer and use it in GitHub Desktop.
Save jimjh/d6abe3b6b0c2a88256d1fc4b3df0da5b to your computer and use it in GitHub Desktop.
TIL that gevent does not switch when you write to a file, even with monkey patching
#! encoding: utf-8
"""Apparently gevent doesn't switch when writing to files on disk."""
from __future__ import print_function
import gevent
from gevent import monkey
from gevent.fileobject import FileObject
monkey.patch_all()
import socket
import tempfile
def doWorkWithFile(string):
print(string + " before")
with tempfile.NamedTemporaryFile() as f:
f.write("this is a long sentence maybe a few bytes" * 10)
f.flush()
f.write("this is a long sentence maybe a few bytes" * 10)
f.flush()
print(string + " after")
def doWorkWithSocket(string):
print(string + " before")
socket.gethostbyname('affirm.com')
print(string + " after")
def doWorkWithGeventFile(string):
print(string + " before")
with tempfile.NamedTemporaryFile() as t, \
FileObject(t, mode='wb', close=False) as f:
f.write("this is a long sentence maybe a few bytes" * 10)
f.flush()
f.write("this is a long sentence maybe a few bytes" * 10)
f.flush()
print(string + " after")
def main():
print('== Control ==')
print('Notice that when using `socket`, the greenlets switch out on block.')
jobs = [gevent.spawn(doWorkWithSocket, string) for string in ['A', 'B', 'C']]
gevent.joinall(jobs, timeout=2)
print('== Treatment ==')
print('Notice that when using `file`, the greenlets do not switch out on block.')
jobs = [gevent.spawn(doWorkWithFile, string) for string in ['A', 'B', 'C']]
gevent.joinall(jobs, timeout=2)
print('== Treatment ==')
print('Notice that even when using `FileObject`, the greenlets do not switch out on block.')
jobs = [gevent.spawn(doWorkWithGeventFile, string) for string in ['A', 'B', 'C']]
gevent.joinall(jobs, timeout=2)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment