PyQT5 show spend time on window - python

I have QMediaPlayer and QVideoWidget for playing videos with spended time QLineEdit and finish program QLineEdit. I am trying to do make a counter that show how many minutes user spended on this program and when user reachs finish program limit pop-up a dialog. For that I create a thread function:
def update_video_timer(self):
end_time = int(self.end_time.text())
start_time = 0
while start_time <= end_time:
self.spended_time.setText(str(start_time))
start_time = start_time + 1
# minutes
time.sleep(60)
# reachs limit
self.mediaPlayer.pause()
dlg = QDialog(self)
dlg.setWindowTitle("YOUR TIME HAS FINISHED!")
dlg.exec_()
I call this function when first video started to play:
from threading import Thread
Thread(target=self.update_video_timer())
But the problem is when video starts to play, program is freezing. Any help and/or improvement of my code is welcome.

Tasks in a GUI must be asynchronous and invoke synchronous tasks that consume very little time. If synchronous tasks consume a lot of time then they must be executed in another thread. In your case it is not necessary to use while + time.sleep() but a counter with a QTimer is enough and thus it is not necessary to use threads:
self.timer = QTimer(self, interval=60 * 1000)
self.timer.timeout.connect(self.on_timeout)
self.start_time = 0
self.end_time = 0
def start(self):
try:
self.end_time = int(self.end_time.text())
except ValueError as e:
print("error")
else:
self.timer.start()
def on_timeout(self):
if self.start_time <= self.end_time:
self.start_time += 1
self.spended_time.setText(str(self.start_time))
else:
self.timer.stop()
self.mediaPlayer.pause()
dlg = QDialog(self)
dlg.setWindowTitle("YOUR TIME HAS FINISHED!")
dlg.exec_()

Related

Fire multiple delays sequentially

My class should take the current start time, and activate the end time by delaying it with clock by calling the function later by the given delay amount. Then I want to fire another clock that delays a print function and embeds the value of the final end-time to it.
However, I cannot yet understand how to fire both sequentially, for example:
from twisted.internet import reactor
from twisted.internet.task import Clock
from twisted.internet.defer import Deferred
#import time
class controlTime:
start_time = time.time()
def end_time(self):
end_time = time.time()
print(end_time)
#time.sleep(3)
return end_time
def delayTime(self, delay=0):
clock = Clock()
clock.callLater(delay, self.end_time)
clock.advance(delay)
def additionalFunc(self, sec):
final_time = sec - self.start_time
print(f'You have delayed for: {final_time} s')
def finalTime(self, g_delay=0):
clock = Clock()
clock.callLater(g_delay, self.additionalFunc, self.end_time())
clock.advance(g_delay)
ctrl = controlTime()
#
#
deferred_signal = Deferred()
deferred_signal2 = Deferred()
dl1 = deferred_signal.addCallback(ctrl.delayTime)
dl1.callback(3)
dl2 = deferred_signal2.addCallback(ctrl.finalTime)
dl2.callback(3)
The issue is that no delay is activated and each function get's fired straight away.
time.sleep can help with the delay, however I want to avoid using this and rely on the built-in delays provided by clock.

how to terminate a thread from within another thread [duplicate]

How can I start and stop a thread with my poor thread class?
It is in loop, and I want to restart it again at the beginning of the code. How can I do start-stop-restart-stop-restart?
My class:
import threading
class Concur(threading.Thread):
def __init__(self):
self.stopped = False
threading.Thread.__init__(self)
def run(self):
i = 0
while not self.stopped:
time.sleep(1)
i = i + 1
In the main code, I want:
inst = Concur()
while conditon:
inst.start()
# After some operation
inst.stop()
# Some other operation
You can't actually stop and then restart a thread since you can't call its start() method again after its run() method has terminated. However you can make one pause and then later resume its execution by using a threading.Condition variable to avoid concurrency problems when checking or changing its running state.
threading.Condition objects have an associated threading.Lock object and methods to wait for it to be released and will notify any waiting threads when that occurs. Here's an example derived from the code in your question which shows this being done. In the example code I've made the Condition variable a part of Thread subclass instances to better encapsulate the implementation and avoid needing to introduce additional global variables:
from __future__ import print_function
import threading
import time
class Concur(threading.Thread):
def __init__(self):
super(Concur, self).__init__()
self.iterations = 0
self.daemon = True # Allow main to exit even if still running.
self.paused = True # Start out paused.
self.state = threading.Condition()
def run(self):
self.resume()
while True:
with self.state:
if self.paused:
self.state.wait() # Block execution until notified.
# Do stuff...
time.sleep(.1)
self.iterations += 1
def pause(self):
with self.state:
self.paused = True # Block self.
def resume(self):
with self.state:
self.paused = False
self.state.notify() # Unblock self if waiting.
class Stopwatch(object):
""" Simple class to measure elapsed times. """
def start(self):
""" Establish reference point for elapsed time measurements. """
self.start_time = time.time()
return self
#property
def elapsed_time(self):
""" Seconds since started. """
try:
return time.time() - self.start_time
except AttributeError: # Wasn't explicitly started.
self.start_time = time.time()
return 0
MAX_RUN_TIME = 5 # Seconds.
concur = Concur()
stopwatch = Stopwatch()
print('Running for {} seconds...'.format(MAX_RUN_TIME))
concur.start()
while stopwatch.elapsed_time < MAX_RUN_TIME:
concur.resume()
# Can also do other concurrent operations here...
concur.pause()
# Do some other stuff...
# Show Concur thread executed.
print('concur.iterations: {}'.format(concur.iterations))
This is David Heffernan's idea fleshed-out. The example below runs for 1 second, then stops for 1 second, then runs for 1 second, and so on.
import time
import threading
import datetime as DT
import logging
logger = logging.getLogger(__name__)
def worker(cond):
i = 0
while True:
with cond:
cond.wait()
logger.info(i)
time.sleep(0.01)
i += 1
logging.basicConfig(level=logging.DEBUG,
format='[%(asctime)s %(threadName)s] %(message)s',
datefmt='%H:%M:%S')
cond = threading.Condition()
t = threading.Thread(target=worker, args=(cond, ))
t.daemon = True
t.start()
start = DT.datetime.now()
while True:
now = DT.datetime.now()
if (now-start).total_seconds() > 60: break
if now.second % 2:
with cond:
cond.notify()
The implementation of stop() would look like this:
def stop(self):
self.stopped = True
If you want to restart, then you can just create a new instance and start that.
while conditon:
inst = Concur()
inst.start()
#after some operation
inst.stop()
#some other operation
The documentation for Thread makes it clear that the start() method can only be called once for each instance of the class.
If you want to pause and resume a thread, then you'll need to use a condition variable.

How to make a pausable timer in python?

I want to create a timer in python with the following functions:
timer.start() - should start the timer
timer.pause() - should pause the timer
timer.resume() - should resume the timer
timer.get() - should return the current time
The timer should run from 0 upwards. It is meant to measure time, not trigger a callback function.
So if you start it, it should start counting the seconds like 0 1 2 3, if you pause it, it should be stilll at 3, but not going further. After its resumed it then goes on with 4 5 6 and so on
How can I do this?
Pause/Resume functions for timer is not a duplicate because I do not care about callbacks.
# mytimer.py
from datetime import datetime
import time
class MyTimer():
"""
timer.start() - should start the timer
timer.pause() - should pause the timer
timer.resume() - should resume the timer
timer.get() - should return the current time
"""
def __init__(self):
print('Initializing timer')
self.timestarted = None
self.timepaused = None
self.paused = False
def start(self):
""" Starts an internal timer by recording the current time """
print("Starting timer")
self.timestarted = datetime.now()
def pause(self):
""" Pauses the timer """
if self.timestarted is None:
raise ValueError("Timer not started")
if self.paused:
raise ValueError("Timer is already paused")
print('Pausing timer')
self.timepaused = datetime.now()
self.paused = True
def resume(self):
""" Resumes the timer by adding the pause time to the start time """
if self.timestarted is None:
raise ValueError("Timer not started")
if not self.paused:
raise ValueError("Timer is not paused")
print('Resuming timer')
pausetime = datetime.now() - self.timepaused
self.timestarted = self.timestarted + pausetime
self.paused = False
def get(self):
""" Returns a timedelta object showing the amount of time
elapsed since the start time, less any pauses """
print('Get timer value')
if self.timestarted is None:
raise ValueError("Timer not started")
if self.paused:
return self.timepaused - self.timestarted
else:
return datetime.now() - self.timestarted
if __name__ == "__main__":
t = MyTimer()
t.start()
print('Waiting 2 seconds'); time.sleep(2)
print(t.get())
print('Waiting 1 second'); time.sleep(1)
t.pause()
print('Waiting 2 seconds [paused]'); time.sleep(2)
print(t.get())
print('Waiting 1 second [paused]'); time.sleep(1)
print(t.get())
print('Waiting 1 second [paused]'); time.sleep(1)
t.resume()
print('Waiting 1 second'); time.sleep(1)
print(t.get())
Run
python mytimer.py
Output
Initializing timer
Starting timer
Waiting 2 seconds
Get timer value
0:00:02.001523
Waiting 1 second
Pausing timer
Waiting 2 seconds [paused]
Get timer value
0:00:03.004724
Waiting 1 second [paused]
Get timer value
0:00:03.004724
Waiting 1 second [paused]
Resuming timer
Waiting 1 second
Get timer value
0:00:04.008578

Clearing the event does not stop other threads

I am working on a scraper that rotates the ips, i have created a small mvp in a notebook that works as expected:
import logging
import time
import random
import threading
from datetime import datetime
from datetime import timedelta
logging.basicConfig(
level=logging.DEBUG,
format='(%(threadName)-10s) %(message)s',
)
class Controller(object):
def __init__(self, event):
self.start_time = datetime.now()
self.event = event
def worker(self):
while True:
if self.event.is_set():
rand_sleep_time = random.randint(1, 10) / 5
logging.debug("Sleeping for %.2f secs" % rand_sleep_time)
time.sleep(rand_sleep_time)
logging.debug("Werking")
else:
time.sleep(1)
def blocker(self):
while True:
rand_sleep_time = random.randint(3, 6)
logging.debug("Sleeping for %.2f secs" % rand_sleep_time)
time.sleep(rand_sleep_time)
if datetime.now() > self.start_time + timedelta(seconds=10):
self.event.clear() # only stop the execution for when the ip is updated
logging.debug("ALL THREADS SLEEP NOW!")
time.sleep(10)
self.event.set() # you can now proceed with the computations
self.start_time = datetime.now()
start_time = datetime.now()
e = threading.Event()
e.set()
c = Controller(e)
for thread in range(NUM_THREADS):
t = threading.Thread(target=c.worker, name='Thread-Worker-{}'.format(thread+1))
t.start()
threading.Thread(target=c.blocker, name='Thread-Blocker-1').start()
So the workers above do some work, then the blocker halts all of them for a brief moment of time, while it updates the "ip", and then the workers start the work again. Taking this logic and implementing it into production, this fails (because I assume the workers do not stop). Unfortunately, I cannot include all of the code, but here is the main part. Hopefully, this is enough, as other parts are not related to the fact that the Ip-Updater does not stop other threads. The only difference in this implementation is that I have used classes, perhaps that should be changed (because the methods have self arg and I'm chaning it. But if the Ip-Updater were to successfully stop the other threads, then there should be no problem, no?):
class ThreadedNewsParser(object):
"""
This little guy parses the news with multiple threads and dynamically changes the ip of the sessions
"""
def __init__(self, update_ip_in, num_threads, date_start, date_end):
assert isinstance(num_threads, int)
assert num_threads > 0
assert any(isinstance(date_start, type_) for type_ in [datetime, date])
assert any(isinstance(date_end, type_) for type_ in [datetime, date])
self.start_time = datetime.now()
self.event = threading.Event()
self.event.set()
self.update_ip_in = update_ip_in
self.check_ip_url = 'https://httpbin.org/ip'
autolog("STARTING WORK ON IP: {}".format(session.get(self.check_ip_url).text), logging.debug)
self.num_threads = num_threads
self.date_start = date_start
self.date_end = date_end
self.dates = [date for date in date_range(date_start, date_end)]
self.p = DailyCompanyNewsParser(2008, 1, 1) # the date here does not matter
def worker(self):
while len(self.dates) > 0:
if self.event.is_set():
print("THREAD WERKING!")
pause = random.randint(1, 5) / 5
autolog('THREAD SLEEPING %.2f' % pause, logging.debug)
time.sleep(pause)
if len(self.dates) > 0:
date = self.dates.pop(0)
self.p.get_news_for_all_stocks(verbose=True, date_=date)
else:
print("THREAD SLEEPING")
time.sleep(10) # so that the threads do not check if the event is set instantaneously
def ip_updater(self): # this is the blocker
while len(self.dates) > 0:
autolog("IP_UPDATER SLEEPING FOR: {}".format(self.update_ip_in / 4), logging.debug)
time.sleep(self.update_ip_in / 4) # do not check the condition every instance
if datetime.now() > self.start_time + timedelta(seconds=self.update_ip_in):
print("ALL THREADS SLEEP NOW!")
autolog("ALL THREADS SLEEP NOW!", logging.info)
self.event.clear() # Make all other threads sleep so that we can update the IP
time.sleep(10)
get_new_ip()
self.start_time = datetime().now()
# autolog("Obtained new IP address: {}".format(session.get(self.check_ip_url).text), logging.debug)
autolog("ALL THREADS WAKE UP NOW!", logging.info)
print("ALL THREADS WAKE UP NOW!")
self.event.set()
def run(self):
for thread in range(self.num_threads):
t = threading.Thread(target=self.worker, name='Thread-Worker-{}'.format(thread+1))
t.start()
threading.Thread(target=self.ip_updater, name='Thread-IPUpdater-1').start()
Rewriting everything so that event and start_time are global variables does not solve the issue.. For example:
class ThreadedNewsParser(object):
"""
This little guy parses the news with multiple threads and dynamically changes the ip of the sessions
"""
def __init__(self, update_ip_in, num_threads, date_start, date_end):
assert isinstance(num_threads, int)
assert num_threads > 0
assert any(isinstance(date_start, type_) for type_ in [datetime, date])
assert any(isinstance(date_end, type_) for type_ in [datetime, date])
self.update_ip_in = update_ip_in
self.check_ip_url = 'https://httpbin.org/ip'
autolog("STARTING WORK ON IP: {}".format(session.get(self.check_ip_url).text), logging.debug)
self.num_threads = num_threads
self.date_start = date_start
self.date_end = date_end
self.dates = [date for date in date_range(date_start, date_end)]
self.p = DailyCompanyNewsParser(2008, 1, 1) # the date here does not matter
def worker(self):
global event
while len(self.dates) > 0:
if event.is_set():
print("THREAD WERKING!")
pause = random.randint(1, 5) / 5
autolog('THREAD SLEEPING %.2f' % pause, logging.debug)
time.sleep(pause)
if len(self.dates) > 0:
date = self.dates.pop(0)
self.p.get_news_for_all_stocks(verbose=True, date_=date)
else:
print("THREAD SLEEPING")
time.sleep(10) # so that the threads do not check if the event is set instantaneously
def ip_updater(self): # this is the blocker
global start_time
global event
while len(self.dates) > 0:
autolog("IP_UPDATER SLEEPING FOR: {}".format(self.update_ip_in / 4), logging.debug)
time.sleep(self.update_ip_in / 4) # do not check the condition every instance
if datetime.now() > start_time + timedelta(seconds=self.update_ip_in):
print("ALL THREADS SLEEP NOW!")
autolog("ALL THREADS SLEEP NOW!", logging.info)
event.clear() # Make all other threads sleep so that we can update the IP
time.sleep(10)
get_new_ip()
start_time = datetime().now()
# autolog("Obtained new IP address: {}".format(session.get(self.check_ip_url).text), logging.debug)
autolog("ALL THREADS WAKE UP NOW!", logging.info)
print("ALL THREADS WAKE UP NOW!")
event.set()
def run(self):
for thread in range(self.num_threads):
t = threading.Thread(target=self.worker, name='Thread-Worker-{}'.format(thread+1))
t.start()
threading.Thread(target=self.ip_updater, name='Thread-IPUpdater-1').start()

Lock with timeout in Python2.7

The accepted solution here doesn't work for all situations,
How to implement a Lock with a timeout in Python 2.7
(In particular the last thread who owns the lock calls cond.notify() when no one holds the conditional variable)
Then, I've tried a spin lock like this:
import threading
import time
class TimeLock(object):
def __init__(self):
self._lock = threading.Lock()
def acquire_lock(self, timeout = 0):
''' If timeout = 0, do a blocking lock
else, return False at [timeout] seconds
'''
if timeout == 0:
return self._lock.acquire() # Block for the lock
current_time = start_time = time.time()
while current_time < start_time + timeout:
if self._lock.acquire(False): # Contend for the lock, without blocking
return True
else:
time.sleep(1)
current_time = time.time()
# Time out
return False
def release_lock(self):
self._lock.release()
However after trying, the spin lock will almost always starve against the blocking lock.
Is there other solutions?
Turns out that python queues have a timeout feature in their
Queue module in 2.7
I can simulate a lock with time out by doing this
lock.acquire() -> Queue.get(block=True, timeout=timeout)
lock.release() -> Queue.put(1, block=False)

Categories