On threads and constructors




I have a class:

class ServerThreadManager(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
# and a bunch of constructor statements

def run(self):
self.ReqHandlingLoop()

# and a bunch of other methods

ServerObj = ServerThreadManager()
print "starting ServerThreadManager"
ServerObj.start()

ServerObj.KeyboardWatcherLoop()

Here's what I want to know:

1) In __init__ I added that other __init__ call from a post here or
from an article on a web page. Does that make sense to do? Why is it
necessary? Do parent constructors always run in Python? If so, before
or after child constructors?

2) Can I assume that constructors run to completion before returning
and that my later call to start() happens after the constructor
finished?

3) I'm having weird problems with an assignment in the constructor that
make me wonder whether I need to know more about thread locks. I do
this:
self.AcceptListenerSocket = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
self.SocketList = [self.AcceptListenerSocket] # We start out
with a single socket in our list.
print "len(self.SocketList) = %d" % len(self.SocketList)
self.SocketPacketFragmentsList = []
print "len(self.SocketPacketFragmentsList) = %d" %
len(self.SocketPacketFragmentsList)

self.SocketPacketFragmentsList.append([self.AcceptListenerSocket,''])
print "len(self.SocketPacketFragmentsList) = %d" %
len(self.SocketPacketFragmentsList)

Note how I do:
self.SocketPacketFragmentsList = []
and then

self.SocketPacketFragmentsList.append([self.AcceptListenerSocket,''])

That works in the sense that I get a length of 1 in the list after the
append. But I originally did this which did not generate a runtime
error but which left the tested list length at 0:
self.SocketPacketFragmentsList =
[[self.AcceptListenerSocket,'']]

The result when tested is len == 0. Why didn't that work? With simpler
types at the Python command line that sort of nested list assignment
worked.

Also, downstream from the append when I test the
self.SocketPacketFragmentsList's length in the started thread it is len
== 0 again. Yet self.SocketList keeps being len == 1 as I expected.

I understand STL and threads over in C++ and write really complex stuff
in C++. But in Python I'm a beginner fighting to do the same sorts of
things and having a rough time of it. Newbieism is no fun.

.



Relevant Pages

  • Inheriting automatic attributes initializer considered harmful?
    ... I'm relatively new to Python, so maybe there is an obvious answer to my ... Writing such constructors for all classes is very tedious. ... So I subclass them from this base class to avoid writing these constructors: ... Is there already a standard lib class doing this? ...
    (comp.lang.python)
  • Re: overloading constructor in python?
    ... could create a class dummie with several constructors, ... takes an int, and one that takes a String as argument. ... So my question is how this is normally solved in python? ... elif isinstance: ...do stuff... ...
    (comp.lang.python)