0

These 3 functions are working, but the last one needs to wait till the first and second are executed. I can use time.sleep(), but I think this is not the right way. How do I fix it?

 def convert_and_save(self):
    self.open()
    time.sleep(5)
    self.convertThread.start()
    time.sleep(5)
    self.saveThread.start()

def convert_and_save(self):
    self.open()
    self.convertThread.start()
    self.saveThread.start()
    self.convertThread.join()
    self.saveThread.join()

error: AttributeError: 'ConvertThread' object has no attribute 'join'


This works but suspendig GUI :(

def convert_and_save(self):
    self.open()
    self.convertThread.start()
    while self.convertThread.isFinished() == False:
        time.sleep(0.1)
    self.saveThread.start()
Luk
  • 185
  • 1
  • 2
  • 10
  • Please include relevant code in your question. – khelwood May 23 '16 at 09:32
  • Question is update of this part of code where is the problem. – Luk May 23 '16 at 09:51
  • Possible duplicate of [python multithreading wait till all threads finished](http://stackoverflow.com/questions/11968689/python-multithreading-wait-till-all-threads-finished) – Ken Y-N May 23 '16 at 23:32
  • @KenY-N I made this in my second example, but I have an error: `AttributeError: 'ConvertThread' object has no attribute 'join'` – Luk May 24 '16 at 07:45

1 Answers1

0

I think you can use

self.convertThread.join()
self.saveThread.join()

to wait until both threads have ended their execution.

Have a look at help("threading.Thread") for more things that you can do with threads.

Whole code:

    def convert_and_save(self):
        self.open()
        time.sleep(5)
        self.convertThread.start()
        time.sleep(5)
        self.saveThread.start()

        self.convertThread.join()
        self.saveThread.join()

        # This is executed when both threads stopped
User
  • 14,131
  • 2
  • 40
  • 59
  • Thak you, but I don't want using `time.sleep` function. Now I have error: `AttributeError: 'ConvertThread' object has no attribute 'join'` Question is updated. – Luk May 23 '16 at 10:26
  • Now I see, what @khelwood means. I assumed a self.convertThread to be a threading.Thread object. That seems not to be the the case. What can a ConvertThread do? Please consider rewriting your question. – User May 23 '16 at 10:31
  • `ConvertThread` converting a lot of strings (changing). And thats why saving (`saveThread`) need wait till `ConvertThread` will be not finish. Question is updated - but this code also suspend GUI. – Luk May 23 '16 at 12:18