Skip to content

Instantly share code, notes, and snippets.

@sccolbert
Created January 22, 2014 23:33
Show Gist options
  • Save sccolbert/8569752 to your computer and use it in GitHub Desktop.
Save sccolbert/8569752 to your computer and use it in GitHub Desktop.
Simple threads with Enaml
from threading import Thread
from time import sleep
from atom.api import Atom, Bool, Int
from enaml.application import deferred_call
from enaml.widgets.api import Window, Container, ProgressBar, PushButton
class Model(Atom):
busy = Bool(False)
value = Int(0)
def worker(model):
p = 0
while p <= 100:
deferred_call(setattr, model, 'value', p)
p += 1
sleep(0.2)
deferred_call(setattr, model, 'busy', False)
def do_it_if_needed(model):
if not model.busy:
model.busy = True
thread = Thread(target=worker, args=(model,))
thread.daemon = True
thread.start()
enamldef Main(Window): main:
title = 'Thread Ticker'
attr model = Model()
Container:
ProgressBar:
value << model.value
PushButton:
text = 'Do It!'
clicked :: do_it_if_needed(model)
@jason-s
Copy link

jason-s commented Nov 2, 2017

This uses deferred_call to set values in model from the worker thread. What about reading values from model? Is that safe to do in a worker thread?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment