spawn problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bierny
    New Member
    • Feb 2007
    • 12

    #1

    spawn problem

    Hi,
    I am quite new to Python but i found it really interesting and useful so I am trying to write even the smallest tools in this language. During this passion of creating everything in Python I get stuck on the following problem.

    I want to create a simple class that will be used as an interface to some application. The class shall start the application as a separate thread and try to connect to it. So, it looks like this
    Code:
    class MyClass:
        
        __init__(self):
            
               self.pid = os.spawnl(os.P_NOWAIT,"some_app","some_app","param1","param2")
    
        # the class ends here
    
    myApp = MyClass
    
    os.waitpid(myApp.pid,0)
    print "bye"
    When I try this approach I fail and I really don't know why. If i move the os.spawn ... statement outside the class it will work and os.waitpid will wait until the process is finished.

    What am I doing wrong? Is it allowed to start separate thread from the constructor of a class?

    Thanks in advance for any help and hints

    Best regards,
    Last edited by bartonc; Feb 24 '07, 03:45 AM. Reason: added [code][/code] tags
  • cybervegan
    New Member
    • Jan 2007
    • 36

    #2
    Originally posted by Bierny
    Hi,
    I am quite new to Python but i found it really interesting and useful so I am trying to write even the smallest tools in this language. During this passion of creating everything in Python I get stuck on the following problem.

    I want to create a simple class that will be used as an interface to some application. The class shall start the application as a separate thread and try to connect to it. So, it looks like this

    class MyClass:

    __init__(self):

    self.pid = os.spawnl(os.P_ NOWAIT,"some_ap p","some_app"," param1","param2 ")

    # the class ends here

    myApp = MyClass

    os.waitpid(myAp p.pid,0)
    print "bye"

    When I try this approach I fail and I really don't know why. If i move the os.spawn ... statement outside the class it will work and os.waitpid will wait until the process is finished.

    What am I doing wrong? Is it allowed to start separate thread from the constructor of a class?

    Thanks in advance for any help and hints

    Best regards,
    Unless it's a typo, I think you are invoking the class incorrectly. You need to say:

    Code:
    myApp = MyClass()
    Note the "()" empty brackets after MyClass: this runs the __init__ for the class and creates a new *instance* of the class. Without this, you are trying to use the *class* object itself (which obviously doesn't work).

    hth,
    -cybervegan

    Comment

    • Bierny
      New Member
      • Feb 2007
      • 12

      #3
      Hi,
      Thanks for quick reply! I just found the cause of the problem. It was, as cybervegan has written, the type of paramters passed to the constructor. Now it works as it should!

      Thanks again!

      Best regards,
      Bierny

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by Bierny
        Hi,
        Thanks for quick reply! I just found the cause of the problem. It was, as cybervegan has written, the type of paramters passed to the constructor. Now it works as it should!

        Thanks again!

        Best regards,
        Bierny
        Go ahead an post some code for everybody's benefit. Only this time, please read the "POSTING GUIDELINES" on the right hand side of the page to learn how to use code tags. Thanks for posting.

        Comment

        Working...