0

I need threading for a voip app that I am writing in python and PyQt5. This thread does the playing ringTone job. But I don't know how to stop this thread from playing, I need a way like terminate in Process to kill the thread clean and easy but I couldn't find that. I have to mention that I can't use Process and multiProcessing (there is a problem that I couldn't find a way to show that in minimal way).

Here is a minimalized code for my problem:

import playsound
# from multiprocessing import Process
from playsound import playsound
from threading import Thread


class Test:
    def __init__(self):
        self.ringTone = True
    def ringTonePlay(self):
        while self.ringTone:
            playsound("ring_tone.wav")

    def testFunction(self):
        self.p1 = Thread(target = self.ringTonePlay)
        self.p1.start()

    def stopFunction(self):
        self.ringTone = False


test = Test()
test.testFunction()
test.stopFunction()

When I call stopFunction function it still keeps ringing. What way you suggest for terminating the playing thread??

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
M.H Mighani
  • 196
  • 5
  • 19
  • You cannot kill threads. Does the thread stop *eventually* after calling ``stopFunction``, once the wav has finished playing? How long is the wav? – MisterMiyagi Jul 25 '19 at 13:08
  • @MisterMiyagi No it doesn't stop..But i need a way to stop the thread..I want the wav to be played until i want – M.H Mighani Jul 25 '19 at 13:09

1 Answers1

0

You can use threading.Event. You can block and unblock the thread as you like.

Edit: If playsound plays the given sound in a different thread, you might need to check the documentation on how to stop/block it.

Example:

import playsound
import threading
from playsound import playsound

class Ringtone(threading.Thread):
    def __init__(self, threadID, name):
      threading.Thread.__init__(self)
      self.threadID = threadID
      self.name = name
      self.event = threading.Event()
    def run(self):
        while True:
            self.event.wait()
            playsound("ring_tone.wav")

ringtone = Ringtone(1, "Ringtone")
ringtone.start()

# To block the thread
ringtone.event.clear()

# To unblock the thread
ringtone.event.set()
  • sounds good..but how can i kill the thread? because in this way the thread isn't killed and the app is still running – M.H Mighani Jul 25 '19 at 13:58
  • The code in the question already checks a flag in the ``while`` loop to see if it needs to stop. Waiting for an event will not stop the sound if the exiting the ``while`` loop did not stop it either. – MisterMiyagi Jul 25 '19 at 16:23
  • Well, killing threads is **not** recommended. Take a look at this [question.](https://stackoverflow.com/a/325528/9250756) Killing the thread might cause hazards like file locks, deadlocks etc. The library used in the question is old and uncomplete. It doesn't provide a **proper** way to stop the playback. You might want to check other packages so take a look at this [question.](https://stackoverflow.com/q/260738/9250756) – Berkay Tekin Öz Jul 26 '19 at 05:34