Example #1
0
    def __init__(self,
                 function,
                 args,
                 kwargs,
                 use_cothread=True,
                 thread_pool=None):
        self.cothread = maybe_import_cothread()
        if use_cothread and not self.cothread:
            use_cothread = False
        self._result_queue = Queue()
        self._result = self.NO_RESULT
        self._function = function
        self._args = args
        self._kwargs = kwargs

        if use_cothread:
            if self.cothread.scheduler_thread_id != get_thread_ident():
                # Spawning cothread from real thread
                self.cothread.Callback(self.cothread.Spawn,
                                       self.catching_function)
            else:
                # Spawning cothread from cothread
                self.cothread.Spawn(self.catching_function)
        else:
            # Spawning real thread
            thread_pool.apply_async(self.catching_function)
Example #2
0
 def __init__(self, use_cothread=True):
     if use_cothread:
         self.cothread = maybe_import_cothread()
     else:
         self.cothread = None
     if self.cothread:
         if self.cothread.scheduler_thread_id == get_thread_ident():
             self._lock = self.cothread.RLock()
         else:
             self._lock = self.cothread.CallbackResult(self.cothread.RLock)
     else:
         self._lock = threading.RLock()
         self._check_cothread_lock = lambda: None
Example #3
0
    def __init__(self, user_facing=False):
        self.user_facing = user_facing
        self.cothread = maybe_import_cothread()
        if self.cothread:
            self._event_queue = self.cothread.EventQueue()
            if user_facing:
                # Install a signal handler that will make sure we are the
                # thing that is interrupted
                def signal_exception(signum, frame):
                    self._event_queue.Signal(self.INTERRUPTED)

                signal.signal(signal.SIGINT, signal_exception)
        else:
            self._queue = queue.Queue()
 def start_poll_loop(self):
     # queue to listen for stop events
     if not self.client.started:
         self._stop_queue = Queue()
         if self.client.started:
             self.client.stop()
         from socket import socket
         if self.use_cothread:
             cothread = maybe_import_cothread()
             if cothread:
                 from cothread.cosocket import socket
         self.client.start(self.spawn, socket)
     if not self._blocks_parts:
         self._make_blocks_parts()
     if self._poll_spawned is None:
         self._poll_spawned = self.spawn(self._poll_loop)
Example #5
0
 def __init__(self, process):
     """
     Args:
         process (Process): The process to use to find child Block
     """
     self._q = self.make_queue()
     # Func to call just before requests are dispatched
     self._notify_dispatch_request = None
     self._process = process
     self._next_id = 1
     self._futures = {}  # dict {int id: Future)}
     self._subscriptions = {}  # dict {int id: (func, args)}
     self._requests = {}  # dict {Future: Request}
     self._pending_unsubscribes = {}  # dict {Future: Subscribe}
     # If not None, wait for this before listening to STOPs
     self._sentinel_stop = None
     self._cothread = maybe_import_cothread()
Example #6
0
 def __init__(self):
     assert not self._instance, \
         "Can't create more than one instance of Singleton. Use instance()"
     self.cothread = maybe_import_cothread()
     if self.cothread:
         # We can use it in this thread
         from cothread import catools
         self.in_cothread_thread = True
     else:
         # We need our own thread to run it in
         q = Queue()
         threading.Thread(target=_import_cothread, args=(q,)).start()
         self.cothread, catools = q.get()
         self.in_cothread_thread = False
     self.catools = catools
     self.DBR_STRING = catools.DBR_STRING
     self.DBR_LONG = catools.DBR_LONG
     self.DBR_DOUBLE = catools.DBR_DOUBLE
     self.FORMAT_CTRL = catools.FORMAT_CTRL
     self.FORMAT_TIME = catools.FORMAT_TIME
     self.DBR_ENUM = catools.DBR_ENUM
     self.DBR_CHAR_STR = catools.DBR_CHAR_STR
Example #7
0
 def setUp(self):
     self.process = Process("proc")
     self.controller = MagicMock()
     self.process.add_controller("block", self.controller)
     self.o = Context(self.process)
     self.cothread = maybe_import_cothread()