The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

Python programming question

shadydentistshadydentist Registered User regular
edited October 2009 in Help / Advice Forum
I have two thread classes, Parent and Child.

The Parent spawns an instance of Child, and needs to know when Child has finished running. Right now, Parent contains a condition variable, is there a way to pass that condition variable to Child?

What I've been trying is
class Child(Thread):
    def __init__(self, Condition c):
        Thread.__init__(self)
        self.monitor=c

    
    def run(self):
        for i in range(0, 5):
            print "hello from child", i


which doesn't work. Any help?

Steam & GT
steam_sig.png
GT: Tanky the Tank
Black: 1377 6749 7425
shadydentist on

Posts

  • vonPoonBurGervonPoonBurGer Registered User regular
    edited October 2009
    I'm far from a Python expert, but I've done something similar in other languages by forking the parent, and you can do that in Python.

    vonPoonBurGer on
    Xbox Live:vonPoon | PSN: vonPoon | Steam: vonPoonBurGer
  • shadydentistshadydentist Registered User regular
    edited October 2009
    I'm far from a Python expert, but I've done something similar in other languages by forking the parent, and you can do that in Python.

    Hm... that would work, but I don't think I'll be able to do that, because the assignment specifies separate Parent and Child classes.

    shadydentist on
    Steam & GT
    steam_sig.png
    GT: Tanky the Tank
    Black: 1377 6749 7425
  • DocDoc Registered User, ClubPA regular
    edited October 2009
        def __init__(self, Condition c):
    

    pretty sure that's not legal python.
        def __init__(self, c):
    

    Doc on
  • shadydentistshadydentist Registered User regular
    edited October 2009
    Yea, sorry, I'm a python newb and still used to Java notation.

    Doc: I tried this, and it doesn't seem to work. Passing the condition from parent to child seems to create a new instance of the condition.

    shadydentist on
    Steam & GT
    steam_sig.png
    GT: Tanky the Tank
    Black: 1377 6749 7425
  • LewishamLewisham Registered User regular
    edited October 2009
    http://www.dalkescientific.com/writings/NBN/threads.html

    This is the best resource on Python threading I've found. Communication between threads is hard. Do it exactly like he says, and it will work (I've done it).

    Lewisham on
  • BarrakkethBarrakketh Registered User regular
    edited October 2009
    I'm far from a Python expert, but I've done something similar in other languages by forking the parent, and you can do that in Python.

    It depends on what you're trying to do. One of the performance-related problems in Python when you use threads is because of the GIL (global interpreter lock). Only one thread may use the interpreter at a time, and the main practical implication of this is that you can't take advantage of multiple cores. While that link mentions thread pools and queues that isn't a solution for that, and manually managing child processes is cumbersome.

    In Python 2.6+ and 3.0+ the multiprocessing module was added which makes dealing with child processes more convenient and lets you do things like create process pools. If you're using the *_async methods of the pool you can specify a callback when the function returns.

    OP: I highly recommend reading this for information on using threads in Python.

    Barrakketh on
    Rollers are red, chargers are blue....omae wa mou shindeiru
  • DocDoc Registered User, ClubPA regular
    edited October 2009
    What you are asking to do is a pretty complicated task.

    Doc on
  • shadydentistshadydentist Registered User regular
    edited October 2009
    Yea, this doesn't seem to be working. I think I might just create a global Condition() variable instead.

    How do you create static global variables in Python?

    shadydentist on
    Steam & GT
    steam_sig.png
    GT: Tanky the Tank
    Black: 1377 6749 7425
  • evilmrhenryevilmrhenry Registered User regular
    edited October 2009
    Yea, this doesn't seem to be working. I think I might just create a global Condition() variable instead.

    How do you create static global variables in Python?
    #same file:
    global variable1
    def function():
        global variable1 #Yes, you define it twice. Skip this, and you can still read the variable, but you can't write.
        variable1=3
    
    #different file
    import file1
    def function2():
        file1.variable1 = 2
    

    evilmrhenry on
Sign In or Register to comment.