
| Current Path : /home/cgabriel/20_dev/12_procpy/dataninja/ |
Linux ift1.ift-informatik.de 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64 |
| Current File : //home/cgabriel/20_dev/12_procpy/dataninja/remoteloops.py |
import threading
from procpy.server import client
class EventLoop():
@staticmethod
def after(delay, function, args):
threading.Timer(delay, function, args=args).start()
class RemoteLoopConnection():
""" either establishes a new connection to the server using given adress, or registers an existing websocket
connection to a client. Once initialized, the after function can be used for remote function execution.
See server/client.py for an example"""
def __init__(self, address=None, ws_con=None):
self.client = client.Client(address) if ws_con is None else client.Client(ws_con=ws_con)
def after(self, delay, function, *args):
def delayed_function():
self.client.call_remote_function(function, args)
threading.Timer(delay, delayed_function).start()
class Conn():
addresses = {
"server": "ws://127.0.0.1:9999/ws"
}
connections = {}
@classmethod
def add_ws_conection(cls, server_id, client_id, ws_con):
cls.connections[server_id +","+client_id] = RemoteLoopConnection(ws_con=ws_con)
return cls.connections[server_id +","+client_id]
@classmethod
def __getitem__(cls, item):
server_id, client_id = item.split(",")
if not item in cls.connections:
address = cls.addresses[server_id]
cls.connections[item] = RemoteLoopConnection(address)
return cls.connections[item]
conn = Conn()
if __name__ == "__main__":
loop = conn["server,01"].after(1, client.hello, "hello")
input()