Click here to Skip to main content
15,887,386 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I want to make a loop that user can stop and resume from, so from the previous question, I implemented the multithreading, but something happens that I have no idea how to fix, so my script comes with a code 0 but when I run it, it just opens and closes immediately.

What I have tried:

Python
import os
import shutil
import requests
import PIL
import threading
from threading import *
import time
import pathlib
import sys
from PIL import Image

 class thread1(Thread):
 def thread():
     .....
  
  def repeat2():
     .....

  while True:
   repeat2()
   t1.start()
   time.sleep(30)

class thread2(Thread):
 def thread():
  print(" ")
  print("Info will now update every 30 seconds (if you want to stop this, type stop)")
  if input() == "stop":
    print ("\nstoping...")
    time.sleep(2)
    print ("Stopped successfully, you can resume by typing resume")
    time.sleep(999999999999999999999999999999999999999999999999999999999999)
    if input() == "resume":
      print("\nResuming...")
      time.sleep(2)

t1 = thread1()
t2 = thread2()

t1.start()
t2.start()
Posted
Updated 1-Oct-23 8:25am
v2

1 solution

That is because your subclaases of Thread do not do anything. See threading — Thread-based parallelism — Python 3.11.5 documentation[^], for the correct way of implementing this.

Your class should be something along the lines of:
Python
class  thread1(Thread):
    def run(self):
        while True:
            print('running ...')
            time.sleep(2)
            if input('continue? ') == 'n':
                break;

t1 = thread1()
t1.start()

And must include the overload of the run method.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900