0

Being a newbie in python and server programming, I am trying to wrap my head around the socketserver and it's threading mixin.

So far i have created a working server that handles ping-requests and can receive multiple files - one file per thread.

Thing is that I want the TCHandler to raise an event with the name of the received file, for further processing by another thread. I can, for the life of me, not wrap my head around how to implement an event in the TCPHandler.

My thought is that I need to insert the event into the TCPHandler when instantiating it, however it is not I that instantiates the handler class, but the ThreadedTCPServer class?

Anyone got any ideas? - Or am I just way, way, way off track here, and need to try a different approach all together?

I could of course just implement the further processing in the TCPHandler, however since i want to pre-process sime images before introducing them to a classifier, I'd rather have the preprocessing and classification running from a queue, and not in the TCPHandler thread.

Please advice

Raveno
  • 71
  • 2

2 Answers2

1

In fact, you only pass a BaseRequestHandler subclass to the TCPServer, because it will create a new request handler instance per request. So if you want to give something to the request handler instances, a simple way would be to make it a class attribute. That way it will be shared among the instances. More or less:

class TCPHandler(BaseRequestHandler):

    event_obj = ...

    def handle(self):
        ...
        event_obj.signal(received_file)
        ...
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • Ah yes, that would be an option, however how would you register eventListeners in the eventHandler, if the eventHandler is instantiated in the TCPHandler class? – Raveno Sep 28 '22 at 07:40
0

I think this actually solves most of my issues.

send a variable to a TCPHandler in python

Further testing is needed, but it looks good so far

Raveno
  • 71
  • 2