Skip to content

Commit 38bdf56

Browse files
author
Ram Rachum
committed
Made sync_workers get called well! Roar!
1 parent da7329a commit 38bdf56

File tree

4 files changed

+76
-33
lines changed

4 files changed

+76
-33
lines changed

src/garlicsim/edgecruncher.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@
3333

3434
#from core import *
3535
import threading
36-
import Queue as queue
36+
37+
try:
38+
import queue
39+
except ImportError:
40+
import Queue as queue
3741

3842
try:
3943
from misc.processpriority import set_process_priority
@@ -52,7 +56,7 @@ class EdgeCruncher(threading.Thread):
5256
most of the smart work is being done by sync_workers.
5357
"""
5458
def __init__(self,starter,step_function,*args,**kwargs):
55-
Process.__init__(self,*args,**kwargs)
59+
threading.Thread.__init__(self,*args,**kwargs)
5660

5761
self.step=step_function
5862
self.starter=starter
@@ -92,21 +96,22 @@ def run(self):
9296
#import psyco #These two belong here?
9397
#psyco.full()
9498

95-
current=self.starter
96-
order=None
99+
current = self.starter
100+
order = None
97101
while True:
98-
next=self.step(current)
102+
next = self.step(current)
99103
self.work_queue.put(next)
100-
current=next
104+
current = next
101105

102106
try:
103107
order=self.message_queue.get(block=False)
104108
#do something with order
105-
if order=="???":
106-
#do ???
107-
pass
108-
109-
order=None
110-
except QueueModule.Empty:
109+
if order=="Terminate":
110+
return
111+
order = None
112+
except queue.Empty:
111113
pass
112114

115+
def terminate(self):
116+
self.message_queue.put("Terminate")
117+

src/garlicsimwx/applicationwindow.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os, sys
2+
import random
23

34
import wx
45

@@ -8,6 +9,7 @@
89
import misc.notebookctrl as notebookctrl
910
import customwidgets
1011

12+
import misc.threadtimer as threadtimer
1113

1214
#import psyco
1315
#psyco.full()
@@ -23,6 +25,11 @@ def get_program_path():
2325
sys.path.append(get_program_path())
2426
########################
2527

28+
29+
wxEVT_RUN_BACKGROUND = wx.NewEventType()
30+
EVT_RUN_BACKGROUND = wx.PyEventBinder(wxEVT_RUN_BACKGROUND, 1)
31+
32+
2633
class ApplicationWindow(wx.Frame):
2734
"""
2835
An application window that allows the user to open multiple GuiProjects
@@ -58,9 +65,19 @@ def __init__(self,*args,**keywords):
5865
self.Bind(wx.EVT_TOOL, self.on_new, id=s2i("Button New"))
5966
self.Bind(wx.EVT_TOOL, self.done_editing, id=s2i("Button Done editing"))
6067

68+
"""
69+
self.Bind(EVT_RUN_BACKGROUND, self.on_run_background)
70+
71+
event = wx.PyEvent()
72+
event.SetEventType(wxEVT_RUN_BACKGROUND)
73+
wx.PostEvent(self, event)
74+
75+
self.run_background_block=False
76+
"""
6177

62-
self.Bind(wx.EVT_IDLE,self.sync_workers_wrapper)
63-
self.idle_block=False
78+
self.background_timer = threadtimer.ThreadTimer(self)
79+
self.background_timer.start(150)
80+
self.Bind(threadtimer.EVT_THREAD_TIMER, self.background_stuff)
6481

6582
self.Show()
6683

@@ -78,6 +95,7 @@ def delete_gui_project(self,gui_project):
7895
"""
7996

8097
def exit(self,e):
98+
self.background_timer.stop()
8199
self.Close()
82100

83101
def done_editing(self,e=None):
@@ -103,27 +121,14 @@ def on_new(self,e):
103121
gui_project=guiproject.GuiProject(simulation_package,self.notebook)
104122
self.add_gui_project(gui_project)
105123

106-
def sync_workers_wrapper(self,e=None):
124+
def background_stuff(self, e=None):
107125
"""
108126
A function that calls `sync_workers` for all the
109127
open GuiProjects.
110128
"""
129+
for gui_project in self.gui_projects:
130+
gui_project.sync_workers()
111131

112-
if self.idle_block==True:
113-
return
114-
115-
116-
for thing in self.gui_projects:
117-
thing.sync_workers()
118-
119-
wx.CallLater(150,self._clear_idle_block_and_do) # Should make the delay customizable?
120-
self.idle_block=True
121-
122-
def _clear_idle_block_and_do(self):
123-
self.idle_block=False
124-
event=wx.PyEvent()
125-
event.SetEventType(wx.wxEVT_IDLE)
126-
wx.PostEvent(self,event)
127132

128133

129134
def main():

src/garlicsimwx/guiproject.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,10 @@ def toggle_playing(self):
194194

195195
def __play_next(self,node):
196196
"""
197-
A function called repeatedly while playing the simualtion.
197+
A function called repeatedly while playing the simulation.
198198
"""
199199
self.show_state(node.state)
200-
self.main_window.Refresh()
200+
self.main_window.Refresh() # Make more efficient?
201201
self.active_node=node
202202
try:
203203
next_node=self.path.next_node(node)
@@ -253,7 +253,7 @@ def sync_workers(self):
253253
"""
254254

255255
if added_nodes>0:
256-
self.main_window.Refresh()
256+
self.tree_modify_refresh()
257257
if self.ran_out_of_tree_while_playing==True:
258258
self.ran_out_of_tree_while_playing=False
259259
self.stop_playing()
@@ -286,3 +286,7 @@ def make_generic_initial_dialog(self):
286286
self.make_plain_root()
287287
initial_dialog.Destroy()
288288

289+
def tree_modify_refresh(self):
290+
self.seek_bar.Refresh()
291+
self.tree_browser.Refresh()
292+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import threading
2+
import time
3+
import wx
4+
5+
wxEVT_THREAD_TIMER = wx.NewEventType()
6+
EVT_THREAD_TIMER = wx.PyEventBinder(wxEVT_THREAD_TIMER, 1)
7+
8+
class ThreadTimer(object):
9+
def __init__(self, parent):
10+
self.parent = parent
11+
self.thread = Thread()
12+
self.thread.parent = self
13+
self.alive = False
14+
15+
def start(self, interval):
16+
self.interval = interval
17+
self.alive = True
18+
self.thread.start()
19+
20+
def stop(self):
21+
self.alive = False
22+
23+
class Thread(threading.Thread):
24+
def run(self):
25+
while self.parent.alive:
26+
time.sleep(self.parent.interval / 1000.0)
27+
event = wx.PyEvent()
28+
event.SetEventType(wxEVT_THREAD_TIMER)
29+
wx.PostEvent(self.parent.parent, event)

0 commit comments

Comments
 (0)