-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathasyncserver.py
More file actions
37 lines (26 loc) · 1.24 KB
/
asyncserver.py
File metadata and controls
37 lines (26 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from .server import RawServer, RawServerCallback
from gevent import pool, Greenlet
class RawAsyncServer(RawServer):
"""
Prefered server instead of RawServer.
Async server using :class:`gevent.Greenlet` in a :class:`gevent.pool` to allow asynchronous calls.
This will ensure you are not loosing data because the handler is too long.
"""
pool = pool.Pool()
def handle_handler(self, handler):
"""Add the Greenlet to the pool with ``handler.run()``, the function to run.
:param handler: The RawRequestHandler provided in the constructor
:type handler: RawRequestHandler"""
self.pool.start(Greenlet(handler.run))
class RawAsyncServerCallback(RawServerCallback, RawAsyncServer):
"""
Async server using :class:`gevent.Greenlet` in a :class:`gevent.pool` to allow asynchronous calls.
"""
def handle_handler(self, handler):
"""Add the Greenlet to the pool with ``self.callback(handler, self)``: the function to run.
:param handler: The RawRequestHandler provided in the constructor
:type handler: RawRequestHandler"""
self.pool.start(Greenlet(self.callback, handler, self))