Basic Python Question
Christopher T King
squirrel at WPI.EDU
Thu Jul 22 10:52:32 EDT 2004
On Thu, 22 Jul 2004, Richard Spooner wrote:
> I have created a threaded class which sets up a socket and then binds to a
> port. When I make a new instance I send the port number and I would like
> the __init__ routine to set up the socket and then attempt to bind it to a
> port.
>
> If I already have an instance of this class running then obviously I'll
> already have a port bound. So if I try and create another instance with the
> same port I'd like the program to flag this error and inform the user that
> it is instead using the original instance..
To get the effect you want (return the previous instance), you'll need to
do some trickery with __new__, the function which is responsible for
creating a class (as opposed to initialization, which is handled by
__init__):
import threading
import socket
import struct
class dataretriever(threading.Thread):
__bound = {} # this holds data of the form port:class_instance
__opened = False # used to stop multiple __init__s
def __new__(cls,port):
# try to get an existing class instance bound to port
try:
c = cls.__bound[port]
# if that fails, make a new instance
except KeyError:
c = threading.Thread.__new__(cls, port)
# and store it in our class variable
cls.__bound[port] = c
# this is executed if the try: succeeded
else:
print 'Port %d already bound, returning previous instance!' % port
# __new__ must return an instance of cls
return c
def __init__(self, port):
# only initialize ourselves once
if self.__opened: return True
self.__opened=True
threading.Thread.__init__(self)
self.setDaemon(1)
self.resultQueue = resultsQueue
self.s = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
try:
self.s.bind(( '', port))
except:
print "Error binding worker"
self.start()
def run(self):
while 1:
pass
#playing with the data I recieve
>>> x = dataretriever(9999)
>>> z = dataretriever(10000)
>>> z is x
False
>>> y = dataretriever(9999)
Port 9999 lready bound, returning previous instance!
>>> y is x
True
Hope this all makes some sense :P
More information about the Python-list
mailing list