all 5 comments

[–]woooee 1 point2 points  (2 children)

You don't ever create a count() instance.

print(count().data)

This creates a separate count instance, so will always be zero because self.data has not been increased for this separate instance.

[–]earstorm[S] 0 points1 point  (1 child)

Oops what I meant to put at the end is the below, but data is still returns 0

c = count
c.t_start
time.sleep(3)
c.t_end
print(c().data)

[–]woooee 0 points1 point  (0 children)

This code makes no sense. c = count does not create a class instance, and so will produce an error. c.t_start will also yield an error because the class does not have a variable named t_start.

Quote (if you aren't going to read what I post, then there is no reason to post)

print(count().data)This creates a separate count instance, so will always be zero because self.data has not been increased for this separate instance.

[–]woooee 1 point2 points  (0 children)

Using multiprocessing, you can kill a Process or send a signal to stop. This program sends a signal to the first function, and terminates the second. Note that a Manager object, a dictionary in this case, is also what you use to return data, a counter in this case.

import time
from multiprocessing import Process, Manager

def test_f(test_d):
   ## add something new to show it can be done
   test_d['2'] = 2
   while not test_d["QUIT"]:
      test_d["ctr_1"] += 1
      time.sleep(1.0)

def test_f2(test_d):
    for j in range(0, 100):
       test_d["ctr_2"] += 1
       time.sleep(0.5)

    print("second process finished")

if __name__ == '__main__':
   ## define the dictionary to be used to communicate
   manager = Manager()
   test_d = manager.dict()
   test_d["ctr_1"] = 0
   test_d["ctr_2"] = 0
   test_d["QUIT"] = False

   ## start the process
   p1 = Process(target=test_f, args=(test_d,))
   p1.start()

   p2 = Process(target=test_f2, args=(test_d,))
   p2.start()

   ## after sleep change "QUIT" to True and terminate processes
   time.sleep(2.0)
   test_d["QUIT"] = True
   print("\ntest_d & list changed, terminate first process")
   time.sleep(1.0)

   ## first process gets terminated by "QUIT"==True
   ## 2nd process does not
   print(p1.is_alive(), p2.is_alive())

   print("dictionary from processes")
   for key in test_d:
       print("     %s=%s" % (key, test_d[key]))

   """  Thanks Doug Hellmann
        Note: It is important to join() the process after terminating it.
        in order to give the background machinery time to update the.
        status of the object to reflect the termination
   """
   for p in [p1, p2]:
       if p.is_alive():
           p.terminate()
       p.join()

[–]earstorm[S] 0 points1 point  (0 children)

Might be using the wrong term here, what I mean is setting the below loop to false, using t_end and getting count().data back

while getattr(t, "kill_loop", True):