Updating Popup.Animated to play gif until external task is completed (PYSimpleGUI) - python

I am looking to create a UI that displays an animated popup while another task is being carried out. That will exit upon completion. I am using PYSimpleGUI and am using the example listed here to base my work off. I can get a single frame of the animation to display once I start the code and exit upon completion of the task but can't get it to play the entire gif. Code:
import queue
import threading
import time
import PySimpleGUI as sg
# ############################# User callable CPU intensive code #############################
# Put your long running code inside this "wrapper"
# NEVER make calls to PySimpleGUI from this thread (or any thread)!
# Create one of these functions for EVERY long-running call you want to make
def long_function_wrapper(work_id, gui_queue):
# LOCATION 1
# this is our "long running function call"
#time.sleep(10) # sleep for a while as a simulation of a long-running computation
x = 0
while True:
print(x)
time.sleep(0.5)
x = x + 1
if x == 5:
break
# at the end of the work, before exiting, send a message back to the GUI indicating end
gui_queue.put('{} ::: done'.format(work_id))
# at this point, the thread exits
return
def the_gui():
gui_queue = queue.Queue() # queue used to communicate between the gui and long-running code
layout = [[sg.Text('Multithreaded Work Example')],
[sg.Text('This is a Test.', size=(25, 1), key='_OUTPUT_')],
[sg.Button('Go'), sg.Button('Exit')], ]
window = sg.Window('Multithreaded Window').Layout(layout)
# --------------------- EVENT LOOP ---------------------
work_id = 0
while True:
event, values = window.Read(timeout=100) # wait for up to 100 ms for a GUI event
if event is None or event == 'Exit':
#sg.PopupAnimated(None)
break
if event == 'Go': # clicking "Go" starts a long running work item by starting thread
window.Element('_OUTPUT_').Update('Starting long work %s'%work_id)
# LOCATION 2
# STARTING long run by starting a thread
thread_id = threading.Thread(target=long_function_wrapper, args=(work_id, gui_queue,), daemon=True)
thread_id.start()
#for i in range(200000):
work_id = work_id+1 if work_id < 19 else 0
#while True:
sg.PopupAnimated(sg.DEFAULT_BASE64_LOADING_GIF, background_color='white', time_between_frames=100)
#if message == None:
#break
# --------------- Read next message coming in from threads ---------------
try:
message = gui_queue.get_nowait() # see if something has been posted to Queue
except queue.Empty: # get_nowait() will get exception when Queue is empty
message = None # nothing in queue so do nothing
# if message received from queue, then some work was completed
if message is not None:
# LOCATION 3
# this is the place you would execute code at ENDING of long running task
# You can check the completed_work_id variable to see exactly which long-running function completed
completed_work_id = int(message[:message.index(' :::')])
sg.PopupAnimated(None)
#window['_GIF_'].update_animation(sg.DEFAULT_BASE64_LOADING_GIF, time_between_frames=100)
#window.read(timeout = 1000)
# if user exits the window, then close the window and exit the GUI func
window.Close()
############################# Main #############################
if __name__ == '__main__':
the_gui()
print('Exiting Program'
)

You've got your call to popup_animated inside of an "if" statement that is only executed once.
You must call popup_animated for every frame you wish to show. It's not spun off as a task that works in the background.
This change to your code will keep the animation going as long as there as background tasks running.
import queue
import threading
import time
import PySimpleGUI as sg
# ############################# User callable CPU intensive code #############################
# Put your long running code inside this "wrapper"
# NEVER make calls to PySimpleGUI from this thread (or any thread)!
# Create one of these functions for EVERY long-running call you want to make
def long_function_wrapper(work_id, gui_queue):
# LOCATION 1
# this is our "long running function call"
# time.sleep(10) # sleep for a while as a simulation of a long-running computation
x = 0
while True:
print(x)
time.sleep(0.5)
x = x + 1
if x == 5:
break
# at the end of the work, before exiting, send a message back to the GUI indicating end
gui_queue.put('{} ::: done'.format(work_id))
# at this point, the thread exits
return
def the_gui():
gui_queue = queue.Queue() # queue used to communicate between the gui and long-running code
layout = [[sg.Text('Multithreaded Work Example')],
[sg.Text('This is a Test.', size=(25, 1), key='_OUTPUT_')],
[sg.Text(size=(25, 1), key='_OUTPUT2_')],
[sg.Button('Go'), sg.Button('Exit')], ]
window = sg.Window('Multithreaded Window').Layout(layout)
# --------------------- EVENT LOOP ---------------------
work_id = 0
while True:
event, values = window.Read(timeout=100) # wait for up to 100 ms for a GUI event
if event is None or event == 'Exit':
# sg.PopupAnimated(None)
break
if event == 'Go': # clicking "Go" starts a long running work item by starting thread
window.Element('_OUTPUT_').Update('Starting long work %s' % work_id)
# LOCATION 2
# STARTING long run by starting a thread
thread_id = threading.Thread(target=long_function_wrapper, args=(work_id, gui_queue,), daemon=True)
thread_id.start()
# for i in range(200000):
work_id = work_id + 1 if work_id < 19 else 0
# while True:
# if message == None:
# break
# --------------- Read next message coming in from threads ---------------
try:
message = gui_queue.get_nowait() # see if something has been posted to Queue
except queue.Empty: # get_nowait() will get exception when Queue is empty
message = None # nothing in queue so do nothing
# if message received from queue, then some work was completed
if message is not None:
# LOCATION 3
# this is the place you would execute code at ENDING of long running task
# You can check the completed_work_id variable to see exactly which long-running function completed
completed_work_id = int(message[:message.index(' :::')])
window.Element('_OUTPUT2_').Update('Finished long work %s' % completed_work_id)
work_id -= 1
if not work_id:
sg.PopupAnimated(None)
if work_id:
sg.PopupAnimated(sg.DEFAULT_BASE64_LOADING_GIF, background_color='white', time_between_frames=100)
# window['_GIF_'].update_animation(sg.DEFAULT_BASE64_LOADING_GIF, time_between_frames=100)
# window.read(timeout = 1000)
# if user exits the window, then close the window and exit the GUI func
window.Close()
############################# Main #############################
if __name__ == '__main__':
the_gui()
print('Exiting Program')

Related

How Can I create an event that run a function each time the event is set?

I would like to set up a system with python3 that would launch a function each time an event is called. I would like to rewrite this javascript code in python
const event = new Event('myevent');
// Listen for the event.
elem.addEventListener('myevent', function (e) { /* ... */ }, false);
// Dispatch the event.
elem.dispatchEvent(event);
I wrote tried this code but the function runs only once
import threading
import time
event = threading.Event()
x = 0
def myFunction():
global x
event.wait()
if event.is_set():
print("event is set : " + str(x))
x += 1
time.sleep(0.3)
th1 = threading.Thread(target=myFunction)
th1.start()
while True:
event.set()
time.sleep(1)
event.clear()
First I will post the right code to solve your problem and then I will explain.
import threading
import time
event = threading.Event()
x = 0
def myFunction():
global x
while True:
event.wait()
if event.is_set():
print("event is set : " + str(x))
x += 1
event.clear()
th1 = threading.Thread(target=myFunction)
th1.start()
while True:
event.set()
time.sleep(1)
An Event object is a mechanism used for communication between threads. event.wait() will wait until the flag is set to sent a signal to thread/s it has been subscribed by. Every time event.wait() is called inside a thread, the thread pauses there until the event's set flag is set.
So if you want to run the
if event.is_set():
print("event is set : " + str(x))
x += 1
block repeatedly, You have to call event.wait() repeatedly once per second. So you have to put it inside a while loop.
The second place where you have done wrongly is , you try to clear the flag inside the same while loop you used to set it. You have to reset the flag just right after the code block was run. So you must place event.clear() just after x += 1. And remove the time.sleep(0.3) line from code.
The result is
event is set : 0
event is set : 1
event is set : 2
event is set : 3
event is set : 4
event is set : 5
event is set : 6
event is set : 7
event is set : 8
event is set : 9
.......

Exchanging keystrokes based data among multiple python processes

I am running two separate Processes in my script. First process, p1, starts a oneSecondTimer routine that executes at exactly at 1 second and does some work. Second process, p2, fires off a keyboard listener that, well, listens to the keyboard.
At the moment, I want the p1 process to stop when the user presses the escape key. I tried using a global variable, it didn't work. I tried using a queue, it worked, but it is definitely not the most elegant solution out there. Its actually an ugly workaround which is not going to scale up.
Eventually, the script would have a number of separate parallel processes that would be controlled (not just start/stop) by pressing various keys.
Here's the code,
import time
from pynput import keyboard
from multiprocessing import Process, Queue
def on_release(key):
if key == keyboard.Key.esc:
print('escaped!')
# Stop listener
return False
def keyboardListener(q):
with keyboard.Listener(on_release=on_release) as listener:
listener.join()
print('Keybord Listener Terminated!!!')
# Make the queue NOT EMPTY
q.put('Terminate')
def oneSecondTimer(q):
starttime = time.time()
# Terminate the infinite loop if
# queue is NOT EMPTY
while (not q.qsize()):
print("tick")
time.sleep(1.0 - ((time.time() - starttime) % 1.0))
return False
if __name__ == '__main__':
q = Queue()
p1 = Process(target=oneSecondTimer, args=(q,))
p1.start()
p2 = Process(target=keyboardListener, args=(q,))
p2.start()
Finally managed to make this work.
In the above snippet, when I was calling the
listener.join()
to the Keyboard Listener event, it was essentially blocking the execution of the rest of the keyboardListener(q) process till the on_release(key) function STOPPED. Because this is exactly what .join() is supposed to do. It is a Blocking call.
In the following snippet, the keyboard.listener thread is simply started in the keyboardListener(q) process. A while loop keeps track of the variable called fetchKeyPress. The variable does what the name implies, it fetches the pressed key in the on_release(key) subroutine. Key presses fetched by fetchKeyPress are pumped in the Queue called q that is shared among the two processes, keyboardListener(key) and oneSecondTimer(q). The keyboardListener process runs 4 times as fast as the oneSecondTimer process, has logic to exit the while loop and prevents flooding of the queue if the user presses the same key continuously/repeatedly.
The oneSecondTimer(q) process runs at every second. If the q is not empty, it spits out whatever is there in the q. It also has a while loop exit logic built into it.
Now I can utilize the data (key presses) acquired by the process p2 and use it in the other parallel running process p1.
p2 is the Producer. p1 is the Consumer.
import time
from pynput import keyboard
from multiprocessing import Process, Queue
fetchKeyPress = 10
def on_release(key):
global fetchKeyPress
fetchKeyPress = key
if key == keyboard.Key.esc:
fetchKeyPress = 0
print('escaped!')
# Stop listener
return False
def keyboardListener(q):
global fetchKeyPress
prevKeyFetch = 10 # Keep track of the previous keyPress
keyboard.Listener(on_release=on_release).start()
while (fetchKeyPress):
print ('Last Key Pressed was ', fetchKeyPress)
# Fill the Queue only when a new key is pressed
if (not (fetchKeyPress == prevKeyFetch)):
q.put(fetchKeyPress)
# Update the previous keyPress
prevKeyFetch = fetchKeyPress
time.sleep(0.25)
print('Keybord Listener Terminated!!!')
q.put('Terminate')
def oneSecondTimer(q):
runner = True # Runs the while() loop
starttime = time.time()
while (runner):
print('\ttick')
if (not q.empty()):
qGet = q.get()
print ('\tQueue Size ', q.qsize())
print ('\tQueue out ', qGet)
# Condition to terminate the program
if (qGet == 'Terminate'):
# Make runner = False to terminate the While loop
runner = False
time.sleep(1.0 - ((time.time() - starttime) % 1.0))
return False
if __name__ == '__main__':
q = Queue()
p1 = Process(target=oneSecondTimer, args=(q,))
p1.start()
p2 = Process(target=keyboardListener, args=(q,))
p2.start()
But I think at the end of the day, I am probably just going to use the https://pypi.org/project/keyboard/ library because of the simplicity. Thanks to #toti08 for the suggestion in the comments above.

multiprocessing script to scan for new values and put in queue not working

Here is my script:
# globals
MAX_PROCESSES = 50
my_queue = Manager().Queue() # queue to store our values
stop_event = Event() # flag which signals processes to stop
my_pool = None
def my_function(var):
while not stop_event.is_set():
#this script will run forever for each variable found
return
def var_scanner():
# Since `t` could have unlimited size we'll put all `t` value in queue
while not stop_event.is_set(): # forever scan `values` for new items
x = Variable.objects.order_by('var').values('var__var')
for t in x:
t = t.values()
my_queue.put(t)
time.sleep(10)
try:
var_scanner = Process(target=var_scanner)
var_scanner.start()
my_pool = Pool(MAX_PROCESSES)
while not stop_event.is_set():
try: # if queue isn't empty, get value from queue and create new process
var = my_queue.get_nowait() # getting value from queue
p = Process(target=my_function, args=("process-%s" % var))
p.start()
except Queue.Empty:
print "No more items in queue"
except KeyboardInterrupt as stop_test_exception:
print(" CTRL+C pressed. Stopping test....")
stop_event.set()
However I don't think this script is exactly what I want. Here's what I was looking for when I wrote the script. I want it to scan for variables in "Variables" table, add "new" variables if they don't already exists to the queue, run "my_function" for each variable in the queue.
I believe I have WAYYYY to many while not stop_event.is_set() functions. Because right now it just prints out "No more items in queue" about a million times.
Please HELP!! :)

Concurrent Threads in Python

I am having issues getting 3 threads to run concurrently. I would like to have the "trade" loop, the "prices"loop and the "stop" loop run at the same time however it seems that the "stop" loop hijacks the program and runs while the others wait their turn. How should I set it up so that they all run at the same time?
import Queue
import threading
import time
import json
from execution import Execution
from settings import STREAM_DOMAIN, API_DOMAIN, ACCESS_TOKEN, ACCOUNT_ID
from strategy import TestRandomStrategy
from streaming import StreamingForexPrices
from event import TickEvent
from rates import stop
def trade(events, strategy, execution):
"""
Carries out an infinite while loop that polls the
events queue and directs each event to either the
strategy component of the execution handler. The
loop will then pause for "heartbeat" seconds and
continue.
"""
while True:
try:
event = events.get(False)
except Queue.Empty:
pass
else:
if event is not None:
if event.type == 'TICK':
strategy.calculate_signals(event)
elif event.type == 'ORDER':
print "Executing order!"
execution.execute_order(event)
time.sleep(heartbeat)
if __name__ == "__main__":
heartbeat = 0 # Half a second between polling
events = Queue.Queue()
# Trade 1000 unit of EUR/USD
instrument = "EUR_USD"
units = 1
stopLoss = stopper
# Create the OANDA market price streaming class
# making sure to provide authentication commands
prices = StreamingForexPrices(
STREAM_DOMAIN, ACCESS_TOKEN, ACCOUNT_ID,
instrument, events
)
#handle stopLoss price
stopper = stop()
# Create the execution handler making sure to
# provide authentication commands
execution = Execution(API_DOMAIN, ACCESS_TOKEN, ACCOUNT_ID)
# Create the strategy/signal generator, passing the
# instrument, quantity of units and the events queue
strategy = TestRandomStrategy(instrument, units, events, stopLoss)
# Create two separate threads: One for the trading loop
# and another for the market price streaming class
trade_thread = threading.Thread(target=trade, args=(events, strategy, execution))
price_thread = threading.Thread(target=prices.stream_to_queue, args=[])
rate_thread = threading.Thread(target=stop, args=[])
# Start both threads
trade_thread.start()
price_thread.start()
rate_thread.start()
Just fyi, everything worked great until I tried to add the "rate". The only things I have added are an additional thread, the stopLoss and the rate.py file.
rate.py:
import oandapy
import time
oanda = oandapy.API(environment="practice", access_token="xxxxxxxxx")
while True:
response = oanda.get_prices(instruments="EUR_USD")
prices = response.get("prices")
asking_price = prices[0].get("ask")
stop = asking_price - .001
print stop
time.sleep(1)
Thanks for the help in advance!
First of all, a remark:
don't use sleep if you can avoid it; for example, in the "trade" loop you
don't need sleep at all if you make a blocking .get() on your queue
Then, once the "rates.py" is imported it starts the while loop; you're
missing the stop() function (or your code is not complete ?)
EDIT: in case you want to add the stop function in rates.py, just put
the while loop code inside a def stop(): block like this
def stop():
while True:
response = oanda.get_prices(instruments="EUR_USD")
prices = response.get("prices")
asking_price = prices[0].get("ask")
stop = asking_price - .001
print stop
time.sleep(1)
(btw: do you really know what you're doing?)

Controlling a python thread with a function

Thanks to those who helped me figure out I needed to use threading to run a loop in a control script I have run, I now have an issue to try and control the thread - by starting or stopping it based on a function:
I want to start a process to get a motor to cycle through a movement based on a 'start' parameter sent to the controlling function, also I want to send a 'stop' parameter to stop the thread too - here's where I got to:
def looper():
while True:
print 'forward loop'
bck.ChangeDutyCycle(10)
fwd.ChangeDutyCycle(0)
time.sleep(5)
print 'backwards loop'
bck.ChangeDutyCycle(0)
fwd.ChangeDutyCycle(20)
time.sleep(5)
def looper_control(state):
t = threading.Thread(target=looper)
if state == 'start':
t.start()
elif state == 'stop':
t.join()
print 'looper stopped!!'
This starts the thread okay when I call looper_control('start') but throws an error when looper_control('stop'):
File "/usr/lib/python2.7/threading.py", line 657, in join
raise RuntimeError("cannot join thread before it is started")
RuntimeError: cannot join thread before it is started
EDIT: looper_control called from here
if "motor" in tmp:
if tmp[-1:] == '0':
#stop both pin
MotorControl('fwd',0,0)
print 'stop motors'
looper_control('stop')
elif tmp[-1:] == '2':
#loop the motor
print 'loop motors'
looper_control('start')
UPDATE: Ive not been able to stop the thread using the method suggested - I thought I had it!
here's where I am:
class sliderControl(threading.Thread):
def __init__(self,stop_event):
super(sliderControl,self).__init__()
self.stop_event = stop_event
def run(self):
while self.stop_event:
print 'forward loop'
bck.ChangeDutyCycle(10)
fwd.ChangeDutyCycle(0)
time.sleep(5)
print 'backwards loop'
bck.ChangeDutyCycle(0)
fwd.ChangeDutyCycle(20)
time.sleep(5)
def looper_control(state,stop_event):
if state == 'start':
t = sliderControl(stop_event=stop_event)
t.start()
elif state == 'stop':
#time.sleep(3)
stop_event.set()
#t.join()
print 'looper stopped!!'
called via:
if tmp[-1:] == '0':
#stop both pin
MotorControl('fwd',0,0)
print 'stop motors'
#stop_thread_event = threading.Event()
print 'stopping thread'
print stop_thread_event
looper_control('stop',stop_thread_event)
elif tmp[-1:] == '2':
#loop the motor
print 'loop motors'
global stop_thread_event
stop_thread_event = threading.Event()
print stop_thread_event
looper_control('start', stop_thread_event)
It looked like a separate thread event was being called by loop and stop, so I thought a global would sort it out but its just not playing ball. When I start the loop - it runs, but when I try to stop it, I get looper stopped!! , but the process just keeps running
Your top-level thread routine will need to become an event handler that listens to a Queue object (as in from Queue import Queue) for messages, then handles them based on state. One of those messages can be a shutdown command, in which case the worker thread function simply exits, allowing the main thread to join it.
Instead of time.sleep, use threading.Timer with the body of the timer sending a message into your event queue.
This is a substantial refactoring. But especially if you plan on adding more conditions, you'll need it. One alternative is to use a package that handles this kind of thing for you, maybe pykka.
To stop a python thread you can use threading.Event()
try this:
class YourClass(threading.Thread):
def __init__(self, stop_event):
super(YourClass, self).__init__()
self.stop_event = stop_event
def run(self):
while not self.stop_event.is_set():
# do what you need here (what you had in looper)
def looper_control(state, stop_event):
if state == 'start':
t = YourClass(stop_event=stop_event)
t.start()
elif state == 'stop':
stop_event.set()
and call to looper_control:
stop_thread_event = threading.Event()
looper_control(state, stop_thread_event)
you only can "start" once a thread
but you can lock and unlock the thread.
the best way to stop and start a thread is with mutex, Example:
#!/usr/bin/python
import threading
from time import sleep
mutex2 = threading.Lock()
#This thread add values to d[]
class Hilo(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
while True:
mutex2.acquire()
#Add values to d[]
d.append("hi from Peru")
mutex2.release()
sleep(1)
d=[];
hilos = [Hilo()]
#Stop Thread
#If you have more threads you need make a mutex for every thread
mutex2.acquire()
#Start treades, but the thread is lock
for h in hilos:
h.start()
#so you need do
#unlock THREAD<
mutex2.release()
#>START THREAD
#Sleep for 4 seconds
sleep(4)
#And print d[]
print d
print "------------------------------------------"
#WAIT 5 SECONDS AND STOP THE THREAD
sleep(5)
try:
mutex2.acquire()
except Exception, e:
mutex2.release()
mutex2.acquire()
#AND PRINT d[]
print d
#AND NOW YOUR TRHEAD IS STOP#
#When the thread is lock(stop), you only need call: mutex2.release() for unlock(start)
#When your thread is unlock(start) and you want lock(stop):
#try:
# mutex2.acquire()
#except Exception, e:
# mutex2.release()
# mutex2.acquire()

Categories