multiprocessing-notes 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. I think I'll spawn a process with multiprocessing.Process,
  2. then make a timer to periodically check if it has finished.
  3. When it does, reactivate the run button and stop the progressbar / spinner.
  4. https://docs.python.org/3/library/multiprocessing.html#multiprocessing.Process.close
  5. Example usage of some of the methods of Process:
  6. >>> import multiprocessing, time, signal
  7. >>> p = multiprocessing.Process(target=time.sleep, args=(1000,))
  8. >>> print(p, p.is_alive())
  9. <Process ... initial> False
  10. >>> p.start()
  11. >>> print(p, p.is_alive())
  12. <Process ... started> True
  13. >>> p.terminate()
  14. >>> time.sleep(0.1)
  15. >>> print(p, p.is_alive())
  16. <Process ... stopped exitcode=-SIGTERM> False
  17. >>> p.exitcode == -signal.SIGTERM
  18. True
  19. #from my hello-world project:
  20. window = builder.get_object("myWindow")
  21. window.connect("destroy", Handlers.myDestroy)
  22. window.connect("destroy", Gtk.main_quit)
  23. #This is used to stop the music from playing if the user closes the window during playback.
  24. def myDestroy(self):
  25. print("Exit window button pressed!")
  26. global exitProcess
  27. if exitProcess is not None:
  28. print("Killing this process: " + str(exitProcess.pid))
  29. kill(exitProcess.pid, signal.SIGKILL)
  30. This is not usefull to me:
  31. https://stackoverflow.com/questions/19033818/how-to-call-a-function-on-a-running-python-thread
  32. from multiprocessing import dummy as multithreading
  33. class SomeClass(object):
  34. def doSomething(self):
  35. pass
  36. def doSomethingElse(self):
  37. pass
  38. someClass = SomeClass()
  39. pool = multithreading.Pool(1)
  40. pool.apply(someClass.doSomething)
  41. pool.apply(someClass.doSomethingElse)
  42. pool.apply(someClass.doSomething)
  43. pool.close()
  44. pool.join()