Basic Python Question

R

Richard Spooner

Hey,

I'm very new to python and am trying to do the following. I may get the
jargon wrong at times but hopefully you can see what I'm trying to do...

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..

So far I have this

import threading
import socket
import struct

class dataretriever(threading.Thread):
def __init__(self, port):
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)
y = dataretriever(9999)

When it tries to define y, I'd like it to say "Port xxxx already in use by
x setting y = x"

Any ideas???

Thanks for your time, seems to be a good little language.

Dave
 
R

Richard Spooner

At the moment it detects the error and prints out "Error binding Worker"
just because it was something I could easily check was working.

I would like it to use the except to

check that it's a bind error,
find the existing instance that is bound to that port
make the new instance that it is attempting to create reference this
existing instance. Thus having the new instance effectively bound to the
port but in fact really linked to the existing instance which is in turn
bound to the port.

Cheers

Dave
 
C

Christopher T King

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

True

Hope this all makes some sense :p
 
P

Peter Abel

Richard Spooner said:
Hey,

I'm very new to python and am trying to do the following. I may get the
jargon wrong at times but hopefully you can see what I'm trying to do...

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..

So far I have this

import threading
import socket
import struct

class dataretriever(threading.Thread):
def __init__(self, port):
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)
y = dataretriever(9999)

When it tries to define y, I'd like it to say "Port xxxx already in use by
x setting y = x"

Any ideas???

Thanks for your time, seems to be a good little language.

Dave

Maybe not a big hit but could be an approach:
.... __port = None
.... __instance = None
.... def __init__(self,port):
.... if port == dataretriever.__port:
.... print 'Error binding worker'
.... self.__dict__ = dataretriever.__instance.__dict__
.... return
.... else:
.... dataretriever.__port = port
.... dataretriever.__instance = self
.... self.do_what_ever_you_want_to_do()
.... def do_what_ever_you_want_to_do(self):
.... print 'I do it'
....
Regards
Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top