Python Priting Out Something While Waiting For Long Output
I created a python file that collect data. After collecting all the data, it will print out "Done.". Sometimes, it might take atleast 3 minutes to collect all the data.
I would like to know how to print something like "Please wait..." for every 30 seconds, and it will stop after collecting all the data.
Can anyone help me please?
You can do it with multi threading, run your collect_data method in a separate thread and monitor this thread in the main thread.
Here is the code example:
import time from threading import Thread def collect_data(): pass t = Thread(target=collect_data) t.start() timeout = 30.0 while t.isAlive(): time.sleep(0.1) timeout -= 0.1 if timeout == 0.0: print 'Please wait...' timeout = 30.0
How To Make Python Wait, In this article I'm going to show you a few different ways to wait. goes some long calculation time.sleep(random() * 5 * 60) # when the then waits for the thread to complete its work and finally prints the So the obvious improvement to the previous solution is to add something inside the while-loop that� Then, you use Python’s await keyword to wait for the output() code to run. await is required here because output() has been marked as an async function, so you can’t call it like you would a normal function. When you run this code, your program will execute await 3 times. The code will wait for 1, 2, and 3 seconds, for a total wait time of 6 seconds.
This may be a duplicate of another question. It seems like you might have two issues: having a timer that triggers at a reliable interval (best handled through threading and the
time.sleep() function), and forcing output while the processor is busy.
For this latter issue, the best tool is
sys.stdout.flush() which pushes output to the current output device (this could be your console, or the iPython notebook output screen). This also works to print output from a function that has been called- useful if your computations are wrapped in another function.
import sys def print_this(mystr): print(mystr) sys.stdout.flush() for i in range(1,100) print_this('hello world!')
Your Guide to the Python print() Function – Real Python, In this step-by-step tutorial, you'll learn about the print() function in Python and The first one is one character long, whereas the second one has no content. message = 'Please wait while the program is loading. A more useful example of the sep parameter would be printing something like file paths:. This is part of my client code of python, and what I expect from this is while this waits for user input, it prints what it receives from server. However, client does not print anything when it is waiting for user input -- it only prints when something has been entered by user.
You can simply creat a label and hide it, during collecting your data you can show it.
class collectdata(QDialog): def init(self, hbrk, *args, **kwargs): super(collectdata, self).init(*args, **kwargs)
self.QBtn = QPushButton() self.setFixedWidth(800) self.setFixedHeight(600) self.QBtn.clicked.connect(self.longprocess) layout = QFormLayout() layout.addRow(self.QBtn) self.lblwait = QLabel("Please Wait ...") layout.addRow(self.lblwait) self.lblwait.hide() def longprocess(self): self.lblwait.show() #.... collect your data self.lblwait.hide()
Python sleep(): How to Add Time Delays to Your Code – Real Python, You'll use decorators and the built-in time module to add Python sleep() Have you ever needed to make your Python program wait for something? You can test how long the sleep lasts by using Python's timeit module: As each thread runs and then sleeps, the logging output is printed to the console. #start and process things, then wait p = subprocess.Popen([data["om_points"], ">", diz['d']+"/points.xml"]) print "Happens while running" p.communicate() #now wait plus that you can send commands to process As stated in the documentation, wait can deadlock, so communicate is advisable.
If the program would know how much data it is getting, you could set it up to function like a progress bar..
Python time sleep(), Python time sleep function is used to add delay in the execution of a program. import time for i in [ .5, .1, 1, 2]: print("Waiting for %s" % i , end='') print(" Below image shows the output produced by above python thread sleep example. Note: print() was a major addition to Python 3, in which it replaced the old print statement available in Python 2. There were a number of good reasons for that, as you’ll see shortly. Although this tutorial focuses on Python 3, it does show the old way of printing in Python for reference.
3. An Informal Introduction to Python — Python 3.8.5 documentation, In the following examples, input and output are distinguished by the presence or absence of prompts (>>> and Start the interpreter and wait for the primary prompt, >>> . In interactive mode, the last printed expression is assigned to the variable _ . This feature is particularly useful when you want to break long strings:. Output: Python is very Popular Language. It is also friendly language. Variable Use: Strings can be assigned to variable say string1 and string2 which can called when using the print statement. Example: str1 = 'Wel' print(str1,'come') Output: Wel come Example: str1 = 'Welcome' str2 = 'Python' print(str1, str2) Output: Welcome Python String
Python Input, Output and Import This tutorial focuses on two built-in functions print() and input() to perform I/O task in Python. Also, you will learn to import modules and use them in your program.
I'm attempting to write a game server and I need it to continue on with the code while listening for a connection. I know this is done with the threading module but I don't know how exactly to do this.
- You can use threading. Do your work in a thread and have the timer in another thread. I would also give you code example but i am on my phone in the car at the stoplights.
- you could use threading. have look at