On threads and constructors

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • techiepundit@futurepundit.com

    #1

    On threads and constructors


    I have a class:

    class ServerThreadMan ager(threading. Thread):
    def __init__(self):
    threading.Threa d.__init__(self )
    # and a bunch of constructor statements

    def run(self):
    self.ReqHandlin gLoop()

    # and a bunch of other methods

    ServerObj = ServerThreadMan ager()
    print "starting ServerThreadMan ager"
    ServerObj.start ()

    ServerObj.Keybo ardWatcherLoop( )

    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.AcceptList enerSocket = socket.socket(s ocket.AF_INET,
    socket.SOCK_STR EAM)
    self.SocketList = [self.AcceptList enerSocket] # We start out
    with a single socket in our list.
    print "len(self.Socke tList) = %d" % len(self.Socket List)
    self.SocketPack etFragmentsList = []
    print "len(self.Socke tPacketFragment sList) = %d" %
    len(self.Socket PacketFragments List)

    self.SocketPack etFragmentsList .append([self.AcceptList enerSocket,''])
    print "len(self.Socke tPacketFragment sList) = %d" %
    len(self.Socket PacketFragments List)

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

    self.SocketPack etFragmentsList .append([self.AcceptList enerSocket,''])

    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.SocketPack etFragmentsList =
    [[self.AcceptList enerSocket,'']]

    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.SocketPack etFragmentsList '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.

  • Peter Hansen

    #2
    Re: On threads and constructors

    techiepundit@fu turepundit.com wrote:[color=blue]
    > I have a class:
    >
    > class ServerThreadMan ager(threading. Thread):
    > def __init__(self):
    > threading.Threa d.__init__(self )
    > # and a bunch of constructor statements
    >
    > def run(self):
    > self.ReqHandlin gLoop()
    >
    > # and a bunch of other methods
    >
    > ServerObj = ServerThreadMan ager()
    > print "starting ServerThreadMan ager"
    > ServerObj.start ()
    >
    > ServerObj.Keybo ardWatcherLoop( )
    >
    > 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?[/color]

    Think of them as "initialize rs" 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.Threa d 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.)
    [color=blue]
    > 2) Can I assume that constructors run to completion before returning
    > and that my later call to start() happens after the constructor
    > finished?[/color]

    Yes, the call to the class (e.g. ServerThreadMan ager()) doesn't return
    until the object is fully constructed and initialized.
    [color=blue]
    > 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:[/color]
    ....[color=blue]
    > Note how I do:
    > self.SocketPack etFragmentsList = []
    > and then
    >
    > self.SocketPack etFragmentsList .append([self.AcceptList enerSocket,''])
    >
    > 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.SocketPack etFragmentsList =
    > [[self.AcceptList enerSocket,'']]
    >
    > The result when tested is len == 0.[/color]

    No, it's not.
    [color=blue]
    > Why didn't that work?[/color]

    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?
    [color=blue]
    > Also, downstream from the append when I test the
    > self.SocketPack etFragmentsList 's length in the started thread it is len
    > == 0 again. Yet self.SocketList keeps being len == 1 as I expected.[/color]

    Something removing the contents from the list? Or rebinding the name?
    Trying printing id(self.SocketP acketFragmentsL ist) 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

    Comment

    Working...