Help a C++ escapee!

Ant antroy at gmail.com
Thu Jun 7 05:36:55 EDT 2007


Hi Simon,

> When run, I come unstuck here:
>
>             self.clientSocket, self.clientAddress = network.accept()
>
> I get a nameError on 'network', yet it is one in the global namespace,
> defined in server.py before CServerThread.Listen() is called.

You have imported everything from socketManager in server.py, but
socketManager knows nothing about any globals defined in the server
module! You'd need to import it in socketManager.py:

from server import network

However this may not work, since you have circular dependencies in
your code - server requires socketManager and vice versa. Even if this
does work it is bad practice, and is an indication that your classes
are in the wrong modules, or that your classes need refactoring
somewhat.

A simple solution would be to pass an instance of the CNetworkManager
into CServerThread. i.e.

...
    def Listen(self):
        self.threadArray =[]
        self.ch = 0
        while self.ch < self.maxClients:
            #Create a thread listening to each socket
            self.newThreadObject = CServerThread(self)
            self.newThreadObject.start()
            self. threadArray.append(self.newThreadObject)
            self.ch=self.ch+1
class CServerThread(threading.Thread):
    def __init__(self, network):
        self.network = network

    def run(self):
        while (1):
            self.clientSocket, self.clientAddress =
self.network.accept()

...

HTH.

--
Ant...

http://antroy.blogspot.com/








More information about the Python-list mailing list