huangyi
2017-12-22 08:43:59 UTC
In long connection game servers, most sockets need to be write in multiple
coroutines, and hopefully asynchronously. So I create a coroutine dedicated
to writing to the socket:
import gevent.queue
def _writer(sock, queue):
for item in queue:
sock:sendall(item)
class asyncsocket(socket):
def __init__(self, *args, **kwargs):
socket.__init__(self, *args, **kwargs)
queue = gevent.queue.Queue()
self._queue = queue
self._writer = gevent.spawn(_writer, self, queue)
def sendall(self, msg):
self._queue.put(msg)
Now i wonder is it too wasteful, if there is a low level api, maybe we can
save the cost of extra coroutine?
coroutines, and hopefully asynchronously. So I create a coroutine dedicated
to writing to the socket:
import gevent.queue
def _writer(sock, queue):
for item in queue:
sock:sendall(item)
class asyncsocket(socket):
def __init__(self, *args, **kwargs):
socket.__init__(self, *args, **kwargs)
queue = gevent.queue.Queue()
self._queue = queue
self._writer = gevent.spawn(_writer, self, queue)
def sendall(self, msg):
self._queue.put(msg)
Now i wonder is it too wasteful, if there is a low level api, maybe we can
save the cost of extra coroutine?
--
You received this message because you are subscribed to the Google Groups "gevent: coroutine-based Python network library" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gevent+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "gevent: coroutine-based Python network library" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gevent+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.