Example geventsendfile.pyΒΆ

 1"""An example how to use sendfile[1] with gevent.
 2
 3[1] http://pypi.python.org/pypi/py-sendfile/
 4"""
 5# gevent-test-requires-resource: sendfile
 6# pylint:disable=import-error
 7from errno import EAGAIN
 8from sendfile import sendfile as original_sendfile
 9from gevent.socket import wait_write
10
11
12def gevent_sendfile(out_fd, in_fd, offset, count):
13    total_sent = 0
14    while total_sent < count:
15        try:
16            _offset, sent = original_sendfile(out_fd, in_fd, offset + total_sent, count - total_sent)
17            #print('%s: sent %s [%d%%]' % (out_fd, sent, 100*total_sent/count))
18            total_sent += sent
19        except OSError as ex:
20            if ex.args[0] == EAGAIN:
21                wait_write(out_fd)
22            else:
23                raise
24    return offset + total_sent, total_sent
25
26
27def patch_sendfile():
28    import sendfile
29    sendfile.sendfile = gevent_sendfile

Current source

Next page: Example portforwarder.py