Commits

uniqx  committed 9cc4c8f

refactored process queues to one single queue to avoid race conditions and make make the code generally more simple to understand

  • Participants
  • Parent commits 422bac8
  • Branches multiprocessing

Comments (0)

Files changed (2)

File pyld/__main__.py

 
 
 
-def __check_file(handle):
-  '''checks if a file has changed and calls __refresh_file if necessary.
-
-  :param dict handle: meta data for a monitored python script
-  '''
-
 
 
 

File pyld/process.py

 import ast
 import time
 
-from multiprocessing import Process, Queue
+from multiprocessing import Process, Queue, Lock
 
 class PyldFile():
 
     '''PyldProcess will execute all files from ``file_name_list``.
     '''
 
-    self.add_file_queue = Queue()
-    self.update_file_queue = Queue()
+    self._queue = Queue()
 
-    Process.__init__(self,args=[self.update_file_queue])
+    Process.__init__(self,args=[self._queue])
     self.files = {}
 
+    for file_name in file_name_list:
+      self.add_file(file_name)
+
   def update_file(self,file_name):
     '''
     '''
+    self._queue.put_nowait('///update///'+file_name)
 
-    self.update_file_queue.put_nowait(file_name)
-
-  def add_file(self,pyld_file):
+  def add_file(self,file_name):
     '''
     '''
-
-    self.add_file_queue.put_nowait(file_name)
+    self._queue.put_nowait('///add///'+file_name)
 
   def run(self):
     '''
     '''
 
+    new_files = []
+
     while True:
 
-      while not self.add_file_queue.empty():
-        file_name = self.add_file.queue.get_nowait()
-        self.files[pyld_file] = PyldFile(file_name)
-        self.files[pyld_file]._execute_init()
+      while not self._queue.empty():
 
-        print '### file %s added' % file_name
+        message = self._queue.get_nowait()
 
-      # check for files to update
-      while not self.update_file_queue.empty():
-        file_name = self.update_file_queue.get_nowait()
-        self.files[file_name]._execute_unload()
-        self.files[file_name]._read_and_compile_code_objects()
-        self.files[file_name]._execute_reload()
+        if '///add///' == message[:9]:
+          file_name = message[9:]
+          new_files.append(file_name)
+          self.files[file_name] = PyldFile(file_name)
+          self.files[file_name]._execute_init()
+
+        if '///update///' == message[:12]:
+          file_name = message[12:]
+          if file_name in new_files:
+            new_files.remove(file_name)
+          else:
+            self.files[file_name]._execute_unload()
+          self.files[file_name]._read_and_compile_code_objects()
+          self.files[file_name]._execute_reload()
 
       for file_name, pyld_file in self.files.iteritems():
         pyld_file._execute_tick()