-1

I've been doing normal python scripts for years, but had to recently dive into GUIs (with Tkinter). Simply put, I have a basic Tk window set up with a Start/Stop button, which should call function Foo(). Function Foo() contains a while True though, which would cause the UI to lock up forever.

I am wondering if I can get some guidance from here. Should I use pythons multiprocessing tools? I was thinking about Twisted / Gevent / threads as well, but I don't think they fit the task (could be wrong).

Thanks!

Shane Reustle
  • 8,633
  • 8
  • 40
  • 51
  • You haven't provided near enough details about what your task is and what your trying to accomplish. – Sean McCully Feb 02 '13 at 22:05
  • What is the real problem you're wanting to do? Foo no doubt is not what you really want to do. If you can describe the true nature of Foo, we might be able to better serve you, because what works for a dummy Foo with an infinite loop might not work for your real problem. – Bryan Oakley Feb 02 '13 at 23:36
  • I'm trying to keep the logic as de-coupled as possible from this UI piece. Secondly, Foo() is constantly monitoring a database for jobs, and then making an external API request. The way that API works is you use a while True to read events as they come in. The loops have small sleeps in them, so its not actually pounding the CPU or truly blocking. – Shane Reustle Feb 02 '13 at 23:44
  • Sounds like a job for tkinter's "after" function. – Gonzo Feb 03 '13 at 14:35
  • Should you really need multithreading you need to do some extra work as (regular) tkinter is not thread safe: http://stackoverflow.com/a/7198960/598794 http://stackoverflow.com/a/14482306/598794 – Gonzo Feb 03 '13 at 14:42
  • @Shane Reustle, you can certainly do this with Twisted. Just set up the tk reactor and use deferToThread() to do your blocking query. This will take care of all the threading and GUI event loop interaction for you. – Rakis Feb 03 '13 at 21:12

1 Answers1

1

I ended up using the multiprocessing library from python. I start Foo() in a new process and start. When the stop button is pressed, it terminates the sub process.

Shane Reustle
  • 8,633
  • 8
  • 40
  • 51