I try again in a new thread, the other went out of hand.

I use this code line to create a n value.
n = random.randint(0, 99), i use this n value as a lap index to my while loop. The loop is in a function called "def run(self)" in a class called "class P(thread)". Each lap create one element that is stored in the global variable "num", "num" have a start value = 0. I use this code to add elements to "num":

while n >= 0:
            x = random.randint(0,99) 
            print x,
            num = num +x
            time.sleep(.30) 
            n = n-1

My second class is called "classC(thread)".
This class is not within "class P(thread)".
This class use m = random.randint(0, 99), i use this m value as a lap index to my while loop in this class. It also have "def run(self)".
Each lap subtract from "num" using this code:

while m>=0:
	    x = random.randint(0,99)
	    print x,	
	    num = num -x
	    if num >= 0:
		time.sleep(.15)
		m = m-1
	    else:
		print ""
		print "Wait, not enough !"
		break
	num = num +x

My question is, how do i make this loop wait until there is enough in "num"? I want the loop in class P(thread) ge more time to create more data in "num" then the 2nd loop will continue.

A bit simpler,how can force loop1 in classP to start again and create more data after the BREAK.

Example: P create these numbers 10 20 30 if i add them i get a value 60. Then C starts and get these numbers 20 20 30 =>70.
Total 60-70= -10 i dont want a negative value ever, i want to create more data instead of a BREAK.

I hope this is clearer and yes i will use locks as well but this is enough for now.

Help please, thanks for your time.

Again, you can use a threading.Condition instance. Here is a program that works with python 2.6 and 3.1. I divided the time.sleep by a factor 10 :)

#!/usr/bin/env python

from threading import Thread, Condition
from contextlib import contextmanager
from random import randint
from time import sleep
import sys

@contextmanager
def acquired(condition):
    condition.acquire()
    try:
        yield condition
    finally:
        condition.release()

num = 0
bound = 1 << 31
numcond = Condition()
pexited = False

class P(Thread):
    def __init__(self):
        Thread.__init__(self)

    def run(self):
        global num, pexited
        n = randint(0, 99)
        while n >= 0:
            x = randint(0, 99)
            with acquired(numcond):
                num += x
                print ("P: +%d --> %d" % (x, num))
                if num >= bound:
                    numcond.notify()
            n -= 1
            sleep(0.030)
        with acquired(numcond):
            pexited = True
            print("P exiting")
            numcond.notify()

class C(Thread):
    def __init__(self):
        Thread.__init__(self)

    def run(self):
        global num, bound
        m = randint(0, 99)
        while m >= 0:
            with acquired(numcond):
                bound = randint(0, 99)
                numcond.wait()
                if num > bound:
                    num -= bound
                    print ("C: -%d --> %d" % (bound, num))
                if pexited:
                    print ("C aborting")
                    return
            m -= 1
            sleep(0.015)
        print ("C exiting")

def main():
    p = P()
    c = C()
    p.start()
    c.start()

if __name__ == "__main__":
    main()

Again, you can use a threading.Condition instance. Here is a program that works with python 2.6 and 3.1. I divided the time.sleep by a factor 10 :)

#!/usr/bin/env python

from threading import Thread, Condition
from contextlib import contextmanager
from random import randint
from time import sleep
import sys

@contextmanager
def acquired(condition):
    condition.acquire()
    try:
        yield condition
    finally:
        condition.release()

num = 0
bound = 1 << 31
numcond = Condition()
pexited = False

class P(Thread):
    def __init__(self):
        Thread.__init__(self)

    def run(self):
        global num, pexited
        n = randint(0, 99)
        while n >= 0:
            x = randint(0, 99)
            with acquired(numcond):
                num += x
                print ("P: +%d --> %d" % (x, num))
                if num >= bound:
                    numcond.notify()
            n -= 1
            sleep(0.030)
        with acquired(numcond):
            pexited = True
            print("P exiting")
            numcond.notify()

class C(Thread):
    def __init__(self):
        Thread.__init__(self)

    def run(self):
        global num, bound
        m = randint(0, 99)
        while m >= 0:
            with acquired(numcond):
                bound = randint(0, 99)
                numcond.wait()
                if num > bound:
                    num -= bound
                    print ("C: -%d --> %d" % (bound, num))
                if pexited:
                    print ("C aborting")
                    return
            m -= 1
            sleep(0.015)
        print ("C exiting")

def main():
    p = P()
    c = C()
    p.start()
    c.start()

if __name__ == "__main__":
    main()

Thanks for the nice example, i will check it out but there must be a simpler way to just call a funktion that linger in another class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.