WebSocket-for-Python icon indicating copy to clipboard operation
WebSocket-for-Python copied to clipboard

message.data becoms None in spawned greenlets

Open dsuch opened this issue 9 years ago • 0 comments

Hi there,

I don't have a self-contained test case but I can easily reproduce the following nevertheless in my project.

In a subclass of ws4py.websocket.WebSocket that is running in a gevent-based server I implemented the received_message method.

This works fine as long as it looks similar to the below:

def received_message(self, message):
    _self._on_received_message(message.data)

However, the moment I spawn the handler function into its own greenlet as below ..

from gevent import spawn

def received_message(self, message):
    spawn(_self._on_received_message, message.data)

.. message.data becomes None somewhere between spawn and the moment _self._on_received_message gets called but at any rate _self._on_received_message receives None, the original data is lost by the time that method is called in its own greenlet.

The workaround is to deepcopy it before spawning the greenlet

from copy import deepcopy
from gevent import spawn

def received_message(self, message):
    spawn(_self._on_received_message, deepcopy(message.data))

dsuch avatar Aug 10 '16 14:08 dsuch