4

How to run multiple processes in python without multithreading? For example consider the following problem:-

We have to make a Gui,which has a start button which starts a function(say, prints all integers) and there is a stop button, such that clicking it stops the function.

How to do this in Tkinter?

3
  • 1
    when you say "another process" do you literally mean another process -- spawning a .exe or equivalent? Or, when you say "process" do you really mean "some other function"? I find it hard to believe you would have a whole separate process just to print integers. Commented Feb 25, 2013 at 11:40
  • I meant some other function. Commented Feb 25, 2013 at 18:14
  • 2
    @Chandan: terminology is critically important when asking technical questions. There is a huge difference between a process and a function in this context. You should edit your question to reflect that you don't intend to spawn other processes. Commented Feb 25, 2013 at 23:55

2 Answers 2

4

Then you need to bind the Button widget with the function which starts the working thread. For example:

import time
import threading
import Tkinter as tk

class App():
    def __init__(self, root):
        self.button = tk.Button(root)
        self.button.pack()
        self._resetbutton()
    def _resetbutton(self):
        self.running = False
        self.button.config(text="Start", command=self.startthread)
    def startthread(self):
        self.running = True
        newthread = threading.Thread(target=self.printints)
        newthread.start()
        self.button.config(text="Stop", command=self._resetbutton)
    def printints(self):
        x = 0
        while self.running:
            print(x)
            x += 1
            time.sleep(1) # Simulate harder task

With the self.running approach, you can end the thread gracefully only by changing its value. Note that the use of multiple threads serves to avoid blocking the GUI while printints is being executed.

I have read this previous question and I suppose why you explicitly asked here for a solution without multithreading. In Tkinter this solution can be used in a scenario where the other threads have to communicate with the GUI part. For example: filling a progressbar while some images are being rendered.

However, as it has been pointed in the comments, this approach is too complex for just printing numbers.

Here you can find a lot of information and more examples about Tkinter.


Edit:

Since your new question has been closed, I'll change the code here to clarify the last point.

Sign up to request clarification or add additional context in comments.

8 Comments

@BryanOakley I totally agree, but I suppose the use of the worker thread is for something more relevant than printing integers - i.e, process a big amount of data, which may block the GUI until it finishes.
the question specifically asks how to do this without multithreading.
@A.Rodas How to do it without using two buttons? I mean can we change the text of the button from 'Start' to 'Stop' when the it starts and 'Stop' to 'Start' when it stops?
@BryanOakley you said that Tkinter doesn't support multithreading, then how does it work?(sorry, if I misunderstood the thing)
@Chandan: as long as nothing in a worker thread attempts to access any Tkinter data structures it will probably work. The problems come from trying to access GUI objects from more than one thread.
|
0

Did you try to used multiprocessing module? Seems to be the one you're looking for.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.