socket io don't send emit in subclasses call by pydispatcher #1250
-
hello, I have a home automation server using openzwave to manipulate home automation elements. openzwave allows me to capture events when, for example, a light bulb turns on, etc. my server contains a zwave network subclass which takes care of retrieving events via pydispatch connection, so I passed the socketio server as an attribute to my zwave class in order to emit an event to the client when openzwave detects the ignition or other of a light bulb. my problem is that I can send the events in the run of my server but not in my zwave class and what's more it interrupts the ping/pong of the server and clients here is some code: server: import eventlet
eventlet.monkey_patch(os=True,select=True,socket=True,thread=True,time=True)
import socketio
import sys
import os
import threading
import time
sys.path.append("..")
import json
socketIoServer = socketio.Server(async_mode='eventlet', cors_allowed_origins="*", ping_timeout=60, logger=True, engineio_logger=True)
app = socketio.WSGIApp(socketIoServer, object)
from homeAutomationServer.homeAutomationEngine.classes.homeAutomationEngine import *
class HomeAutomationServer(socketio.Namespace):
"""
class representing the home automation server:
property:
methods:
server event:
"""
homeAutomationEngine = False
running = False
def __init__(self, scriptPath):
self.scriptPath = scriptPath
self.configFilePath = scriptPath + "/configs/homeAutomationServerConfig.json"
self.load_home_automation_engine(self)
socketio.Namespace.__init__(self, '/HomeAutomationServer')
@staticmethod
def load_home_automation_engine(self):
HomeAutomationServer.homeAutomationEngine = HomeAutomationEngine(self.scriptPath + '/homeAutomationEngine', socketIoServer)
###BASE METHODS###
def start(self):
succes = False
if self.serverConfigured == True:
if self.start_engine():
succes = True
else:
succes = False
else:
succes = False
if succes:
self.running = True
return succes
def run(self):
listen_clients = threading.Thread(target=self.listen_clients)
listen_clients.start()
while True:
#HomeAutomationServer.homeAutomationEngine.zWaveNetwork.send_light_controller_color_updated_event(False)
time.sleep(0.1)
###CLIENTS REQUESTS###
"""CONNECTION EVENT"""
@socketIoServer.event(namespace='/HomeAutomationServer')
def connect(sid, environ):
print('client connecté ', sid) zwave network method sending the event: def send_light_controller_color_updated_event(self, event):
succes = False
if self.server != False:
try:
self.server.emit('light_controller_color_updated', {}, namespace='/HomeAutomationServer')
try:
self.server.sleep(0.1)
except:
pass
succes = True
except:
succes = False
else:
succes = False
if succes:
print('iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii')
return succes client code: import eventlet
eventlet.monkey_patch(os=True,select=True,socket=True,thread=True,time=True)
import threading
import socketio
import sys
import os
import json
import time
sys.path.append("..")
from homeAutomationSystem.homeAutomationSystem.classes.homeAutomationSystem import *
homeAutomationSystemSocket = socketio.Server(cors_allowed_origins="*")
app = socketio.WSGIApp(homeAutomationSystemSocket, object)
homeAutomationServerSocket = socketio.Client(logger=True, engineio_logger=True)
class HomeAutomationSystemServer(socketio.Namespace):
homeAutomationSystem = False
def __init__(self, scriptPath):
self.scriptPath = scriptPath
self.configFilePath = scriptPath + '/configs/homeAutomationSystemConfig.json'
self.running = False
socketio.Namespace.__init__(self, '/HomeAutomationSystem')
def listen_home_automation_server(self):
homeAutomationServerSocket.wait()
#listenClient.join()
@homeAutomationServerSocket.event(namespace='/HomeAutomationServer')
def light_controller_color_updated(data):
print("yesssssssssssssssssss") for all the code of the program: https://github.com/tetrew88/homeAutomation |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 11 replies
-
Please do not write issues on this package when you need help fixing your own application. The issues board is for bugs in this package. The discussions board is for asking questions and requesting assistance. Thank you. This time I have converted your message for you. I don't know what zwave is so I cannot comment on that. If the emits that do not work are being issued from a sub-process, then they are not going to work. In that case see the documentation on how to work with auxiliary processes that need to emit. A default server instance server is not designed to work with sub-processes that emit. |
Beta Was this translation helpful? Give feedback.
-
sorry I thought I was in the right section I am a beginner "programmer" who more or less learned as a freelancer. so let me explain to you, I have a zwave network class as an attribute in my server. this class uses openzwave this class receives the socketio server as an attribute in order to use the evoie function. As for openzwave, this allows me to trigger one of the functions of my zwave network class when the color of a light fixture changes and in this function I use the server attribute (socketio server) in order to send the event. what I have difficulty understanding is that if I call the method of my zwave network class directly from the run method of the server everything works correctly. but if it is called by the openzwave trigger it sends the event in the logs and is notified to me but the client does not receive the event and interrupts the ping/pong of the server/client so I don't think we can really say that I'm asking to send it from an external program or a subprocess because it's the instance of the socketio server that is being called |
Beta Was this translation helpful? Give feedback.
-
with flask and the example you gave me it works perfectly, I don't know how to thank you, you're great, you've solved 1 month of hassle and headaches over the code, I thank you very much :DDDDD, obviously it's It is indeed an eventlet which created conflicts. |
Beta Was this translation helpful? Give feedback.
If you use asyncio, then you should use
socketio.AsyncServer
. If you don't use asyncio, then you should usesocketio.Server
. The choice of which of these to use depends on other libraries and packages that you use. If your other libraries are async, then use the asyncio server. If your other libraries are not async, then use the regular server. The rule is not not mix up sync and async packages, as that is very likely not going to work.For the regular server, you can use an eventlet server, a gevent server, or the gunicorn server. The gunicorn server itself can be used in threaded mode, in eventlet mode and in gevent mode. It is also possible to use the Flask development web server in th…