I am creating a Python application that includes socket communication with a server. I would like to have a module which can be used throughout my entire application (several other modules). Currently my module look like this:
class SocketCommunication:
def __init__(self):
self.socketIO = SocketIO(settings.ADDRESS, settings.PORT, Namespace)
def emit(self, message, data):
json_data = json.dumps(data.__dict__)
self.socketIO.emit(message, json_data)
class Namespace(BaseNamespace):
def on_connect(self):
print '[Connected]'
def on_disconnect(self):
print "[Disconnected]"
When I use this in other modules I do the following:
import SocketCommunication
self.sc = SocketCommunication()
The problem is that every time I do this, a new connection is created which will be displayed as a new client on the server, and that is undesirable. From what I can read, Singletons should be avoided in Python and therefore I am curious about what is best practice for this type of problem?