Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > On threads and constructors

Reply
Thread Tools

On threads and constructors

 
 
techiepundit@futurepundit.com
Guest
Posts: n/a
 
      12-31-2005

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.

 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      12-31-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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?


Think of them as "initializers" rather than constructors, since the
object is already created by the time they are called. In any case,
they are never run implicitly if you define an __init__ in the child
class: you must run them explicitly. Since threading.Thread does
important things in its initializer, you must call it as that web page
noted.

(Note that because of this, *you* can control when you call the parent
initializer, whether at the start, end, or middle of your child class
initializer.)

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


Yes, the call to the class (e.g. ServerThreadManager()) doesn't return
until the object is fully constructed and initialized.

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

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


No, it's not.

> Why didn't that work?


It did work. Try it again. If it still looks like it's not
working, either you're not really doing that, or you are doing the test
incorrectly. How and where are you finding the length?

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


Something removing the contents from the list? Or rebinding the name?
Trying printing id(self.SocketPacketFragmentsList) immediately after
creating the list, then again later where you think the length is now
zero. If they show the same value, something is removing the contents
of the list between the two prints.

-Peter

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reg constructors/copy constructors inheritance srp113 C++ 3 02-17-2009 04:01 PM
Is the possible to have all the public constructors of the publicbase class as the constructors of a derived class? Peng Yu C++ 5 09-19-2008 10:19 AM
compiler synthesized constructors/copy constructors/assignment operators Jess C++ 5 06-07-2007 11:09 AM
Copy constructors, de/constructors and reference counts Jeremy Smith C++ 2 08-02-2006 11:25 PM
Constructors that call other Constructors Dave Rudolf C++ 12 02-06-2004 03:26 PM



Advertisments