Jump to content

Python Tkinter start stop thread causes cannot start process twice. how to fix?

Joveice

So I want to start a thread and stop it with different buttons, which I got working. Now the issue is if you stop the thread and start it again you get "cannot start a process twice", how can I solve this, etc how can I recreate the process after it's been stopped with tkinter buttons.

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

can you post the relevant code? My GUESS is that you could just create a new instance of whatever thread object you are doing.

e.g.

someThreadAbstraction = None

whenStartButtonClicked():
    someThreadAbstraction = makeThread()
    someThreadAbstraction.start()
    print("Thread started!")
    
whenStopButtonClicked():
    if (someThreadAbstraction): # make sure we don't stop it twice
        someThreadAbstraction.stop
        someThreadAbstraction = None
        print("Thread stopped!")
    

instead of:

someThreadAbstraction = makeThread();

whenStartButtonClicked():
    someThreadAbstraction.start()
    print("Thread started!")
    
whenStopButtonClicked():
    someThreadAbstraction.stop()
    print("Thread stopped!")
    

 

Again, I don't know what threading library you are using, but my guess is that you make a new instance each time you won't have any "cannot start a process twice" issues, which seems to indicate a thread object is being reused when it's not supposed to be.

 

Either way, a relevant code snippet will help give an actual answer instead of a complete guess

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Cooler: Custom water loop (420mm rad + 360mm rad)

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

 

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

Case: HAF 932 adv

 

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, reniat said:

can you post the relevant code? My GUESS is that you could just create a new instance of whatever thread object you are doing.

e.g.


someThreadAbstraction = None

whenStartButtonClicked():
	someThreadAbstraction = makeThread()
  	someThreadAbstraction.start()
    print("Thread started!")
    
whenStopButtonClicked():
	if (someThreadAbstraction): // make sure we don't stop it twice
  		someThreadAbstraction.stop
  		someThreadAbstraction = None
        print("Thread stopped!")
    

instead of:


someThreadAbstraction = makeThread();

whenStartButtonClicked():
  	someThreadAbstraction.start()
    print("Thread started!")
    
whenStopButtonClicked():
    someThreadAbstraction.stop()
    print("Thread stopped!")
    

 

Again, I don't know what threading library you are using, but my guess is that you make a new instance each time you won't have any "cannot start a process twice" issues, which seems to indicate a thread object is being reused when it's not supposed to be.

 

Either way, a relevant code snippet will help give an actual answer instead of a complete guess

class MyProcess(object):

    def __init__(self):
        self.process = multiprocessing.Process(target=self.run, args=())
        self.process.daemon = True

    def start(self):
        self.process.start()

    def stop(self):
        self.process.terminate()

    def run(self):
        while True:
            print('hello')
            time.sleep(1)

controller = MyProcess()

def start(event):
    controller.start()
    button_start.config(state='disabled')
    button_stop.config(state='enabled')


def stop(event):
    controller.stop()
    button_start.config(state='enabled')
    button_stop.config(state='disabled')


button_start.bind('<Button-1>', start)
button_stop.bind('<Button-1>', stop)

But someone suggested to add this

 def stop(self):
        self.process.stop()
        self.process = self.process = multiprocessing.Process(target=self.run, args=())

Which worked like I expected it to work when calling start after stopping it.

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

Glad that you got it working!

 

I don't know if you need 

self.process = self.process = multiprocessing.Process(target=self.run, args=())

instead of just 

self.process = multiprocessing.Process(target=self.run, args=())

but I don't have the deepest python knowledge so maybe that is doing something I'm not aware of (i'd be surprised though)

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Cooler: Custom water loop (420mm rad + 360mm rad)

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

 

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

Case: HAF 932 adv

 

Link to comment
Share on other sites

Link to post
Share on other sites

19 minutes ago, reniat said:

Glad that you got it working!

 

I don't know if you need 


self.process = self.process = multiprocessing.Process(target=self.run, args=())

instead of just 


self.process = multiprocessing.Process(target=self.run, args=())

but I don't have the deepest python knowledge so maybe that is doing something I'm not aware of (i'd be surprised though)

The bottom works, I didn't try the top one.

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×