Domanda

In short, my question is: how do I know when some particular Python package is compatible with gevent (at least with gevent.monkey.patch_all)?

Actually I was wondering if http-parser library is compatible with gevent (will it block all eventlets somewhere or not), but having a general answer will be better.

È stato utile?

Soluzione

If the library you want to use is built using something that gevent.monkey.patch_all() patches, then it will probably work.

patch_all() will make the standard library play nice with gevent. For example, the socket module is part of the standard library and is patched by patch_all() or patch_socket(), thus any library that is built using sockets should probably maybe work.

It would seem like http-parser uses the socket module, and should thus be compatible with gevent. The only way to know for sure is to test.

Here's an example test, implemented from the example of http-parser github:

from gevent.monkey import patch_all; patch_all()
from gevent.socket import create_connection
import gevent
from http_parser.http import HttpStream
from http_parser.reader import SocketReader

def worker(n):
    try:
        s = create_connection(('gunicorn.org', 80))
        s.send("GET / HTTP/1.1\r\nHost: gunicorn.org\r\n\r\n")
        r = SocketReader(s)
        p = HttpStream(r)
        print "Worker {}, headers length: {}".format(n, len(p.headers()))
    finally:
        s.close()

if __name__ == '__main__':
    jobs = [gevent.spawn(worker, job_no) for job_no in range(10)]
    gevent.joinall(jobs)

Which outputs:

(test)msvalkon@Lunkwill:/tmp$ python test_http_parser.py 
Worker 8, headers length: 4
Worker 1, headers length: 4
Worker 5, headers length: 10
Worker 2, headers length: 10
Worker 9, headers length: 10
Worker 4, headers length: 10
Worker 3, headers length: 10
Worker 6, headers length: 10
Worker 7, headers length: 10
Worker 0, headers length: 10

It seems that http-parser is compliant with gevent.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top