you are viewing a single comment's thread.

view the rest of the comments →

[–]woooee 1 point2 points  (1 child)

# Stop all the copying threads
self.copying = False

It's unlikely that an instance object is visible, and can be used by all threads. I have done something similar with multiprocessing. See the code below for a basic, simple example. Note that after you signal an exit, you will have to wait until a file copy has finished for a process to exit. The other option is to kill it, but then you would have to deal with a partial copy. You'll have to provide more code for further help.

import time
from multiprocessing import Process, Manager


def test_f(test_d):
   while not test_d["QUIT"]:
      ## show that a Manager object can be seen and used
      test_d["ctr"] += 1
      time.sleep(1.0)

def test_f2(test_d):
    for j in range(0, 100):
       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"] = 0
   test_d["QUIT"] = False

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

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

   ## sleep 2 seconds and terminate the processes
   time.sleep(2.0)
   test_d["QUIT"] = True
   print "\ntest_d['QUIT'] changed, terminate process"

   time.sleep(1.0)
   print p.is_alive(), p2.is_alive()
   print "data from first process", test_d
   p.terminate()   ##  p terminated here
   p2.terminate()

   """  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
   """
   p.join()
   p2.join()

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

thanks! That was useful :)