Python: Output while waiting for input [duplicate] - python

This question already has an answer here:
Exiting while loop by pressing enter without blocking. How can I improve this method?
(1 answer)
Closed 5 years ago.
So I currently have a thread that is outputting, and I need a way to handle input while still allowing output from the thread in the meantime.
Currently I have something like this
def thread_func():
while True:
print("Information")
threading.Thread(target=thread_func).start()
while True:
command = raw_input("Enter a command:")
#dostuff with command
My issue now is that my thread isn't printing anything. Or if it is it isn't showing up
Edit:
Found a solution here
Exiting while loop by pressing enter without blocking. How can I improve this method?

You want to use a Queue. New input will go into the back of the queue, and work will be started from the front.

Related

How to make python NOT wait for user input while in a loop? [duplicate]

This question already has answers here:
Python async: Waiting for stdin input while doing other stuff
(3 answers)
Closed 4 months ago.
I want it so that my code continuously replaces itself continuously, and while that executes, I want the input to just take what it is without needing to press enter. I'm an android user and I've already tried downloading non-built-in-modules, so I can't use the keyboard module or anything like that.
My code is basically:
while game == "on":
print("string")
inpu = input
time.sleep(1)
clear screen
Apologies for false text editing or bad "code grammar."
I'm new to coding and stack overflow.
NOT wait for user input while in a loop?
Solution
That is easy, using multi-thread or multi-process. i.e create another thread that handles the input separately.
Demo of full code
Here is a very simple demo of multi-thread to begin.
import _thread
args = None
# Define a function for the thread
def mythread(threadname):
global args
args = input("input anything: ")
# Create one threads as follows
try:
_thread.start_new_thread( mythread, ("UI",) )
while args==None: # mimic your work loop
pass;
print("main loops end demo end")
except:
print ("Error: unable to start thread")
To run
$ python3 demo.py
Output
input anything: hello
main loops end demo end

Making a python script that continues on enter [duplicate]

This question already has answers here:
How do I wait for a pressed key?
(13 answers)
Closed 2 years ago.
I was making a terminal-type program with python and I was wondering if I could make it print the next line of text after you press enter
e.g
`print('this was a triumph')
#press enter to print the next line
print('Im making a note here, huge success!')`
You could try:
input('this was a triumph')
input('Im making a note here, huge success!')
or:
print('this was a triumph')
input('Press Enter To Continue:')
print('Im making a note here, huge success!')

input in timed while loop [duplicate]

This question already has answers here:
Python: How to get input from console while an infinite loop is running?
(3 answers)
Closed 2 years ago.
When I try to allow the user to input a command in a timed while loop (0.1 seconds) it pauses at the input and doesn't get to the time.sleep() until the user enters something. For example:
while True:
[something happens]
userInput = input("Type something")
if userInput = "foo":
[another thing happens]
time.sleep(0.1)
I want the loop to run normally if there is no user input and process the user input if there is without pausing the entire loop.
Calling the input function requires it to wait until the user types something.

Why doesn't my piece of code execute in order? [duplicate]

This question already has answers here:
Printing on the same line with time.sleep()
(2 answers)
Closed 3 years ago.
While trying to solve a bigger issue, I reduced my code to two simple commands and I found out that they are not executed in order.
My idea is to give a kind of feedback to the user after clicking on a button(3D Slicer) while running a function. So, what I would expect is to freeze the button immediately after clicking it until the action is finished. So I tried the following to check if the very first command is executed on first position:
def onStartSegmentation(self):
self.segmentButton.setEnabled(False)
sleep(3)
print("2nd step: Starting segmentation")
However, the result is sleeping for 3 seconds and then executing both commands immediately one after the other.
I know this might sound silly, but I can't guess why it's acting like that.
print() function is buffered. That means the output will be buffered before displaying to the screen until the buffer is full or new line character is encountered. If you want to have your output to be displayed on the screen immediately, you need to flush the buffer by using sys.stdout.flush() or you need to explicitly specify it in the argument to print function.
def onStartSegmentation(self):
print("1st step: Starting segmentation", flush = True)
sleep(3)
print("2nd step: Starting segmentation", flush = True)
OR
def onStartSegmentation(self):
print("1st step: Starting segmentation")
sys.stdout.flush()
sleep(3)
print("2nd step: Starting segmentation")
sys.stdout.flush()

Raw Input with a time limit [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
raw_input and timeout
I was wondering how I can make my raw_input have a time limit.
I want something like raw_input=("What do you want?")
and then if an input isn't given in less than 5 seconds, it'd print "too late"
I've been trying to read up, but it doesn't seem like anything like this is available for raw_input
Use a timer object, setting it to the required number of seconds; and just use tje timer function to display the message you want once the event object is invoked.

Categories

Resources