user502039
user502039

Reputation:

Python: Is there a way I can run mainloop() in the background?

Is there a way I can run mainloop() in the background? I don't want to add the complexity of threads. Thanks in advance.

Upvotes: 2

Views: 1110

Answers (3)

Brandon
Brandon

Reputation: 3764

What about keeping your message loop in the foreground, and your other processing in the background (say, with an after method)? Especially if you don't want to use threads. However, I think threads or processes will probably work better in the long run.

Upvotes: 0

David Heffernan
David Heffernan

Reputation: 612894

No, you can't do this. The message loop runs in its thread and processes your message queue.

There isn't really any official distinction between background and foreground threads. The thread that processes your message is typically called the foreground but it's not really any different from any other thread. It's only convention that leads us to refer to threads foreground or background.

Ultimately, that main thread with the message loop in has the message loop at the top of its call stack and that's just the way it has to be.

It is possible to start a long running task and get it to frequently process messages, but this way requires a keen understanding of re-entrancy issues and often leads to insanity.

If you want long running background tasks then you probably need threads.

Upvotes: 2

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798566

No. The main loop of a GUI framework must always run in the foreground.

Upvotes: 3

Related Questions