Discussion:
[gevent] Write to socket from multiple coroutine asynchronously.
huangyi
2017-12-22 08:43:59 UTC
Permalink
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?
--
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.
huangyi
2017-12-22 08:57:01 UTC
Permalink
圚 2017幎12月22日星期五 UTC+8䞋午4:43:59huangyi写道
Post by huangyi
In long connection game servers, most sockets need to be write in multiple
coroutines, and hopefully asynchronously. So I create a coroutine dedicated
import gevent.queue
sock:sendall(item)
socket.__init__(self, *args, **kwargs)
queue = gevent.queue.Queue()
self._queue = queue
self._writer = gevent.spawn(_writer, self, queue)
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?
Sorry for the temporarily buggy code, it should be like his:
import gevent.queue
def _writer(sock, queue):
for item in queue:
sock.sendall(item)

class asyncsocket(object):
def __init__(self, sock):
self._sock = sock
self._queue = gevent.queue.Queue()
self._writer = gevent.spawn(_writer, self._sock, self._queue)

def sendall(self, msg):
self._queue.put(msg)

def close(self):
self._writer.kill()
--
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.
Loading...