Created a gui with three buttons, one to execute a program, one to halt program, and one to clear the textbuffer . Also have a textview to display text from program. Here are the pertenant functions.
def clear_display(self, widget):
global textbuffer
start = textbuffer.get_start_iter()
end = textbuffer.get_end_iter()
textbuffer.delete(start, end)
self.print_display("")
def start_program(self):
global textbuffer
global mythread
global run
i = 0
while run:
msg = "Count = %s\n" % i
self.print_display(msg)
print ( "Count = %s\n" % i)
i += 1
mythread._Thread__stop()
def start_run(self, widget):
global run
global mythread
run = 1
mythread = Thread(target=self.start_program, args=())
mythread.start()
def stop_run(self, widget):
global run
global textbuffer
msg = "Halt Run\n"
self.print_display(msg)
run = 0
def print_display(self, msg):
global textbuffer
start = textbuffer.get_start_iter()
end = textbuffer.get_end_iter()
text = textbuffer.get_text(start, end)
text = text + msg
textbuffer.set_text(text)
The problem I am having is getting the data to display to the textview. Initial run will produce a partial display. If I press the button clear to the display then press the start button, I will receive some data and some data from the previous display interleaved which should have been deleted. Can anyone see what I am doing incorrectly?
You'll want to use a queue.Queue I'll leave your implementation to you, but the classic use is:
import threading, queue
q = queue.Queue()
def make_something(q, num_things_to_make):
for _ in num_things_to_make
new_thing = make_a_thing()
q.put(new_thing)
def use_something(q):
while True:
thing_to_use = q.get()
do_a_thing(thing_to_use)
q.task_done()
producer = threading.Thread(target=lambda: make_something(q, 8))
producer.start()
for _ in range(num_workers):
worker = threading.Thread(target=lambda: use_something(q))
worker.daemon = True
worker.start()
q.join() # block until all tasks are done
Related
Hi i need to create 2 threads one which repeatedly writes the time of day as an
HH:MM:SS string into a global variable 100 times per second. The second thread will repeatedly read the time of day
string from that variable twice per second and try to display it to screen but code in that thread should ensure the same
string is never written twice in a row. The result is that second thread really displays to screen only once per second. i have tried following code but its not working
import threading
import time
c = threading.Condition()
flag = 0 #shared between Thread_A and Thread_B
val = ''
class Thread_A(threading.Thread):
def __init__(self, name):
threading.Thread.__init__(self)
self.name = name
def run(self):
global flag
global val #made global here
while True:
c.acquire()
if flag == 0:
time.sleep(0)
flag = 1
a=range(1,101)
for i in a:
val=time.strftime("%H:%M:%S", time.localtime(time.time()))
c.notify_all()
else:
c.wait()
c.release()
class Thread_B(threading.Thread):
def __init__(self, name):
threading.Thread.__init__(self)
self.name = name
def run(self):
global flag
global val #made global here
while True:
c.acquire()
if flag == 1:
#time.sleep(1)
flag = 0
a=range(0,2)
for i in a:
print str(val)
#val = 20
c.notify_all()
else:
c.wait()
c.release()
a = Thread_A("myThread_name_A")
b = Thread_B("myThread_name_B")
b.start()
a.start()
a.join()
b.join()
You're making this more complicated than it needs to be. You can use a simple Lock object to make sure that only one thread can access val at a time.
The code below will run on Python 2 or Python 3. To stop it, hit Enter
import time
from threading import Thread, Lock
# Rename Python 2's raw_input to input
try:
input = raw_input
except NameError:
pass
val = ''
lock = Lock()
def set_time(delay=0.01):
''' Write the current time to val '''
global val
while True:
lock.acquire()
val = time.strftime("%H:%M:%S")
lock.release()
time.sleep(delay)
def get_time(delay=0.5):
''' Read the current time from val and print
it if it hasn't been printed already
'''
oldval = ''
while True:
lock.acquire()
if val != oldval:
print(val)
oldval = val
lock.release()
time.sleep(delay)
# Start the threads
for func in (set_time, get_time):
t = Thread(target=func)
t.setDaemon(True)
t.start()
#Wait until we get some input
s = input()
some typical output
02:22:04
02:22:05
02:22:06
02:22:07
02:22:08
The context:
I'm building a Graphical Interface with Qt creator and the "behaviour" file in python. A test version of my GUI is:
The expected behaviour:
I am running 2 different threads which are referred to the same function with different input arguments. With the SELECTOR button I can assign the value of 1 or 2 to a variable (and display it)
The button Start thread enables the correct thread to start (the first time).
The loop should be turned off by the stop button by modifying the global running variable.
This is my code
# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui, uic
import sys
import threading
import time
import Queue
running = False
first_thread = None
second_thread = None
form_class = uic.loadUiType("simple2.ui")[0]
q = Queue.Queue()
select = 0
def action(string, queue): #function called by threads
global running
while(running):
phrase = string
if queue.qsize() < 10:
queue.put(phrase)
#else:
# print queue.qsize()
class MyWindowClass(QtGui.QMainWindow, form_class):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.setupUi(self)
#buttons
self.startButton.clicked.connect(self.start_clicked)
self.stopButton.clicked.connect(self.stop_clicked)
self.selector.clicked.connect(self.sel_click)
#variables
self.first = False
self.second = False
#queue
self.timer = QtCore.QTimer(self)
self.timer.timeout.connect(self.update_phrase)
self.timer.start(1)
def start_clicked(self): #start button callback
global select
if select > 0:
global running
running = True
print "started"
if (not self.first) & (select == 1):
first_thread.start()
self.first = True
if (not self.second) & (select == 2):
second_thread.start()
self.second = True
self.startButton.setEnabled(False)
self.startButton.setText('Starting...')
def stop_clicked(self): #stop button callback
global running
running = False
print "stopped"
self.startButton.setEnabled(True)
self.startButton.setText('Start Thread')
def sel_click(self): #selector button callback
global select
if select < 2:
select = select + 1
else:
select = 1
self.thread_counter.setText(str(select))
def update_phrase(self): #looping function
global running
if (not q.empty()) & running:
self.startButton.setText('Thread on')
abc = q.get()
print abc
def closeEvent(self, event):
global running
running = False
if __name__ == "__main__":
first_thread = threading.Thread(target=action, args = ("first", q))
second_thread = threading.Thread(target=action, args = ("second", q))
app = QtGui.QApplication(sys.argv)
w = MyWindowClass(None)
w.setWindowTitle('Multiple threads test in python')
w.show()
app.exec_()
For now, each thread should simple print on terminal their arguments ("First" or "Second").
If threads are started for the first time, my code works. But I would like to switch between threads infinite times.
Since threads cannot be stopped, is there a way to "pause" them?
I cannot find a solution, I hope someone will help me also with a piece of code. Thank you in advance
You can use Lock class to do that, a simple example would be:
import threading
lock = threading.Lock()
//here it will be lock
lock.acquire() # will block if lock is already held
...
then in other side do
//this will wake up
lock.release()
you can read more here http://effbot.org/zone/thread-synchronization.htm
I have a pyqt gui and calling a long process (ffmpeg) which I put on a separate thread to not block the gui. I then want to update a progress bar when one command of a longer list of commands finishes. The problem is, that I can't call a function in the gui thread out of the worker thread. So I let run a ticker in the worker thread, but when I update the progress bar with a while loop and reading the ticker value, the gui gets blocked again. How can I solve this. I used currently python threading and not Qthread.
Thx for any help!
import threading, pexpect
self.cmd_list = ['ffmpeg -i file outfile','and so on']
self.stop_proc = False
self.executeCMD()
def spawn_ffmpeg_cmd(self):
for cmd in self.cmd_list:
if self.stop_proc == False:
thread = pexpect.spawn(cmd)
print "\nstarted: %s" % cmd
cpl = thread.compile_pattern_list([pexpect.EOF,"frame= *\d+ fps=*\d+",'(.+)'])
while True:
i = thread.expect_list(cpl, timeout=None)
if i == 0: # EOF
print "the sub process exited"
self.pgticker += 1
break
elif i == 1:
frame_number_fps = thread.match.group(0)
print frame_number_fps
thread.close
elif i == 2:
pass
self.startButton.setEnabled(True)
def executeCMD(self):
self.startButton.setEnabled(False)
self.pgticker = 0
threading.Thread(target=self.spawn_ffmpeg_cmd, name="_proc").start()
def stopprocess(self):
self.stop_proc = True
self.cmd_list = []
os.system('pkill ffmpeg')
self.pgticker = len(self.cmd_list)
self.startButton.setEnabled(True)
def updateProgress(self):
pgfactor = 100 / len(self.cmd_list)
progress = 0.0
progress = pgfactor*int(self.pgticker)
self.progressBar.setProperty("value", progress)
In short: Move to QThread and use Qt's signals and slots, they are the preferred way to communicate between threads.
This answer provides some examples how this could look like:
https://stackoverflow.com/a/6789205/2319400
In your case, using the "SomeObject" version from the above could look like this:
class Worker(QtCore.QObject):
madeProgress = QtCore.pyqtSignal([int])
finished = QtCore.pyqtSignal()
def __init__(self, cmdlist):
self.cmdlist = cmdlist
def run(self):
for icmd, cmd in enumerate(self.cmdlist):
# execute your work
# processCommand(cmd)
# signal that we've made progress
self.madeProgress.emit(icmd)
# emit the finished signal - we're done
self.finished.emit()
Then move this worker to a QThread instance you create.
Following the pattern from the linked answer, you can then connect the madeProgress signal
to the setValue slot of a progressbar:
workerThread = QThread()
workerObject = Worker(cmdlist)
workerObject.moveToThread(workerThread)
workerThread.started.connect(workerObject.run)
workerObject.finished.connect(workerThread.quit)
# create a progressbar with min/max according to
# the length of your cmdlist
progressBar = QProgressBar()
progressBar.setRange(0, len(cmdlist))
# connect the worker's progress signal with the progressbar
workerObject.madeProgress.connect(progressBar.setValue)
# start the thread (starting your worker at the same time)
workerThread.start()
I have a gui with buttons to start a loop and another button to stop the loop. I use a global variable - run for the loop. 1 to loop and 0 to stop loop. Below is the code I am using.
import pygtk
pygtk require('2.0')
import gtk
import threading
import time
run = 0
mythread = ""
class daqGui:
def start_program(self):
global run
print ("start program")
i = 0
while run:
print ("Count = %s\n" % i)
i += 1
time.sleep(2)
def start_run(self, widget):
global run
global mythread
run = 1
mythread = threading.Thread(target=self.start_program, args=())
mythread.start()
def stop_run:
global run
print("Halt loop")
run = 0
def __init__(self):
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.connect("delete_event", self.delete_event)
self.window.set_border_width(10)
self.window.set_size_request(600,400)
table = gtk.Table(10, 6, True)
self.window.add(table)
run_button = gtk.Button("Start Run")
run_button.connect("clicked", self.start_run)
run_button.set_size_request(100,30)
table.attach(run_button, 0, 1, 0, 1, False, False, xpadding=0, ypaddig=0)
run_button.show()
stop_button = gtk.Button("Halt Run")
stop_button.connect("clicked", self.stop_run)
stop_button.set_size_request(100,30)
table.attach(stop_button, 2, 3, 0, 1, False, False, xpadding=0, ypaddig=0)
stop_button.show()
def main():
gtk.main()
if __name__=="__main__":
daqGui()
main()
What happens when you run this code, click on the Start Button, you will see the program display i = 1, i = 2 and so forth. Click on the stop button, the program does not stop. Exit the gui, and the loop still continues. You have to kill the process to stop it. What am I doing wrong?? Thanks!
Global variables are sketchy with multithreaded code, and especially with a GUI.
The following code starts a "clock" thread which outputs the time every second.
After two seconds, a 2nd thread sets the stop_event flag, which is shared between both threads.
When the clock thread checks the event object, it sees that it's been flagged, so the loop exits. Generally, the event hasn't been set, so it waits for 1.0 seconds then reruns the loop.
source
import threading, time
def display_clock(event):
while not event.wait(1):
print 'now:',time.strftime('%X')
print("stop_event set")
def stop_clock(event):
time.sleep(3)
event.set()
def main():
stop_event = threading.Event()
threading.Thread(
target=display_clock, args=[stop_event],
).start()
threading.Thread(
target=stop_clock, args=[stop_event],
).start()
# wait for all threads to die
for th in threading.enumerate():
if th != threading.current_thread():
th.join()
if __name__ == "__main__":
main()
output
now: 15:17:16
now: 15:17:17
now: 15:17:18
stop_event set
I want to modify the code below to clear the Entry text prior to new text being written. Basically I want to delete text, wait one second, then write new text. This should give the appearance of "NEW" text being written.
Any ideas? TIA - Brad
import thread, Queue, time, random, poster
from Tkinter import *
dataQueue = Queue.Queue()
def status(t):
try:
data = dataQueue.get(block=False)
except Queue.Empty:
pass
else:
t.delete(0, END)
time.sleep(1)
t.insert(0, '%s\n' % str(data))
t.after(2, lambda: status(t))
def makethread():
thread.start_new_thread(poster.poster, (1,dataQueue))
if __name__ == '__main__':
root = Tk()
root.geometry("240x45")
t = Entry(root)
t.pack(side=TOP, fill=X)
Button(root, text='Start Epoch Display',
command=makethread).pack(side=BOTTOM, fill=X)
status(t)
root.mainloop()
In another file called poster
import random, time
def poster(id,que):
while True:
delay=random.uniform(5, 10)
time.sleep(delay)
que.put(' epoch=%f, delay=%f' % (time.time(), delay))
Since there are potentially many threads writing to the queue (one for every time the button is pressed) it is a little unclear when text should be deleted and new text should be inserted. For example, if text has just been written and new text arrives, should the new text be written immediately or should it be added to a queue for later display as time permits?
You can setup the status handler to process delete commands as well as insert commands. This version of the handler starts a thread after every insert that sends back a delete command. If the ID of the delete command matches the ID of the text currently being displayed, then the status handler erases the display:
def status(t, current_id, queue):
try:
data = queue.get(block = False)
# Insert text for ID command:
if type(data) == tuple:
(id, str) = data
t.delete(0, END)
t.insert(0, str)
current_id = id
# Thread that sends a delete command
# after a fixed delay.
make_delete_thread(id, queue)
# Delete text for ID command:
elif data == current_id:
t.delete(0, END)
except Queue.Empty:
pass
t.after(10, lambda: status(t, current_id, queue))
def make_delete_thread(id, queue):
thread.start_new_thread(delete_thread, (id, queue))
def delete_thread(id, queue):
time.sleep(1)
queue.put(id)
Made these changes and it works... Thanks to #anonakos. See my comments to his answer.
Main code:
else:
t.delete(0, END)
time.sleep(1)
t.insert(0, '%s\n' % str(data))
t.after(2, lambda: status(t))
Poster code:
def poster(id,que):
while True:
delay=random.uniform(5, 10)
time.sleep(delay-0.5)
que.put(' ')
time.sleep(.5)
que.put(' epoch=%f, delay=%f' % (time.time(), delay))