Getting threads to share data correctly in python -


created gui 3 buttons, 1 execute program, 1 halt program, , 1 clear textbuffer . have textview display text program. here 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     = 0     while run:         msg = "count = %s\n" %         self.print_display(msg)          print ( "count = %s\n" % 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 having getting data display textview. initial run produce partial display. if press button clear display press start button, receive data , data previous display interleaved should have been deleted. can see doing incorrectly?

you'll want use queue.queue i'll leave implementation you, classic use is:

import threading, queue  q = queue.queue()  def make_something(q, num_things_to_make):     _ 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()  _ in range(num_workers):     worker = threading.thread(target=lambda: use_something(q))     worker.daemon = true     worker.start()  q.join() # block until tasks done 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -