1

I'm learning python for fun and what I'm trying to achieve here is a program that constantly asks user for input through a While True loop, while still running other scripts on background (Chosen by the user).

What I currently have works fine... But not exactly as I wanted. It takes the user's input and starts threads like asked, but pauses them while waiting for the user's next input. Then, it prints what my thread should print every second, but only once, after the console's next print.

I'm using IDLE to test my code regularly, maybe that could help.

Principal script:

from mythreadclass import MyThread

while True:
    user_input = input("What do I do ? ")

    if user_input == "start thread":
        #Will start a threat that prints foo every second.
        mythread = MyThread()
        mythread.start()

    elif user_input == "hello":
        print("Hello !")

    else:
        print("This is not a correct command.")

Thread class:

import time
from threading import Thread, Timer

class MyThread(Thread):
    def __init__(self):
        Thread.__init__(self)

    def run(self):
        print("Thread started !")
        while True:
            self.foo()
            time.sleep(1)

    def foo(self):
        print("foo !")

What I have when executing:

What do I do ?
>>> start thread
Thread started !
What do I do ?
>>> hello
foo !Hello !
>>> hello
foo !Hello !

As you can see, the thread should print foo! every second while still asking user for the next input. Instead, it starts the thread and prints foo ! only when the user types something: input pauses/blocks the thread.

I don't know if what I want to achieve is clear so here it is:

What do I do ?
>>> start thread
Thread started !
What do I do ?
foo !
foo !
foo !
#User waited 3 seconds before typing hello so there should be 3 foos
>>> hello
Hello !
What do I do ?
foo !
foo ! 

etc etc.
6
  • I changed that input to raw_input and it all seems to work as expected. And I put everything in one file. Commented Dec 6, 2016 at 0:52
  • The way you have it written here works perfectly for me in python 3.5. Is this exactly what you have written or an example? Commented Dec 6, 2016 at 0:54
  • @JustinBell It's a (very) simplified example, but my whole project is globally the same (A principal class that casts a thread in an other file) Commented Dec 6, 2016 at 0:56
  • @sal I'm in Python 3.5, raw_input() was changed to input() ! Commented Dec 6, 2016 at 0:59
  • Ok. But same as JustinBell, this works for me in python 2.7. Commented Dec 6, 2016 at 1:20

1 Answer 1

0

You're doing the Thread wrong. Instead of infinite recursion, the run() method must be an infinite loop like that:

from threading import Thread
import time

class MyThread(Thread):
    def __init__(self):
        Thread.__init__(self)
        self.start()

    def run(self):
        while (True):
            self.foo()
            time.sleep(1)

    def foo(self):
        print("foo !")

my = None

while True:
    user_input = raw_input("What do I do ? ")

    if user_input == "start thread":
        #Will start a threat that prints foo every second.
        if my == None: 
            print("Starting thread...")
            my = MyThread()
        else:
            print("Thread is already started.")

    elif user_input == "hello":
        print("Hello !")

    else:
        print("This is not a correct command.")
Sign up to request clarification or add additional context in comments.

3 Comments

It's probably true that infinite recursion will eventually lead to a problem (a stack overflow, conveniently enough :P) but I don't think that's the immediate problem @sohomangue is facing
I didn't know using timer infinitely was wrong, I'll fix this ! However, using ˋtime.sleep()` didn't change the output, the program still waits for the next console print to print "foo !" instead of printing it every second.
I modified my code so it works for me like you want it to.

Your Answer

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