2

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?

Zul Hazmi
  • 329
  • 2
  • 5
  • 13
  • 1
    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. – Alexandru Godri Aug 07 '15 at 05:55
  • 1
    you could use threading. **[have look at](http://www.tutorialspoint.com/python/python_multithreading.htm)** – macf00bar Aug 07 '15 at 06:02

4 Answers4

2

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
Muhammad Tahir
  • 5,006
  • 1
  • 19
  • 36
0

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.

Sample code:

import sys

def print_this(mystr):
   print(mystr)
   sys.stdout.flush()    

for i in range(1,100)
   print_this('hello world!')
Community
  • 1
  • 1
emunsing
  • 9,536
  • 3
  • 23
  • 29
0

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()
Hon
  • 1
  • 1
-3

If the program would know how much data it is getting, you could set it up to function like a progress bar..