def __init__(self, latfactory, settings, chanprefix, impact_exe, data_dir, work_dir=None): if not isinstance(latfactory, LatticeFactory): raise TypeError("VirtualAccelerator: Invalid type for LatticeFactory") self._latfactory = latfactory if not isinstance(settings, dict): raise TypeError("VirtualAccelerator: Invalid type for accelerator Settings") self._settings = settings self._chanprefix = chanprefix self.impact_exe = impact_exe self.data_dir = data_dir self.work_dir = work_dir self._epicsdb = [] self._csetmap = OrderedDict() self._elemmap = OrderedDict() self._fieldmap = OrderedDict() self._readfieldmap = OrderedDict() self._noise = 0.001 self._started = False self._continue = False self._rm_work_dir = False self._ioc_process = None self._ioc_logfile = None self._subscriptions = None self._lock = cothread.Event(False)
def _get_one(self, name, request=None, timeout=5.0, throw=True): done = cothread.Event(auto_reset=False) def cb(value): assert not done, value # spurious second callback if isinstance(value, (RemoteError, Disconnected, Cancelled)): done.SignalException(value) else: done.Signal(value) cb = partial(cothread.Callback, cb) op = super(Context, self).get(name, cb, request=request) _log.debug('get %s request=%s', name, request) try: ret = done.Wait(timeout) except cothread.Timedout: ret = TimeoutError() if throw: raise ret finally: op.close() return ret
def _import_cothread(q): import cothread from cothread import catools from cothread.input_hook import _install_readline_hook _install_readline_hook(None) q.put((cothread, catools)) # Wait forever cothread.Event().Wait()
def import_cothread(self): import cothread from cothread import catools from cothread.input_hook import _install_readline_hook _install_readline_hook(None) self.event = cothread.Event() self.q.put((cothread, catools)) self.event.Wait()
def __init__(self, pv, count): self.count = count self.accum = [] self.done = cothread.Event() self.monitor = camonitor(pv, self.add_value, format=FORMAT_TIME, all_updates=True)
def __init__(self, at_lattice, callback=None, emit_calc=True): """ .. Note:: To avoid errors, the physics data must be initially calculated here, during creation, otherwise it could be accidentally referenced before the attributes _emitdata and _lindata exist due to delay between class creation and the end of the first calculation in the thread. Args: at_lattice (at.lattice_object.Lattice): An instance of an AT lattice object. callback (callable): Optional, if passed it is called on completion of each round of physics calculations. emit_calc (bool): Whether or not to perform the beam envelope based emittance calculations. **Methods:** """ if (not callable(callback)) and (callback is not None): raise TypeError("If passed, 'callback' should be callable, {0} is " "not.".format(callback)) self._at_lat = at_lattice self._rp = numpy.ones(len(at_lattice) + 1, dtype=bool) self._emit_calc = emit_calc # Initial phys data calculation. if self._emit_calc: self._at_lat.radiation_on() self._emitdata = self._at_lat.ohmi_envelope(self._rp) self._at_lat.radiation_off() self._lindata = self._at_lat.linopt(refpts=self._rp, get_chrom=True, coupled=False) self._radint = self._at_lat.get_radiation_integrals( 0.0, self._lindata[3]) # Threading stuff initialisation. self._queue = cothread.EventQueue() # Explicitly manage the cothread Events, so turn off auto_reset. self._paused = cothread.Event(auto_reset=False) self.up_to_date = cothread.Event(auto_reset=False) self.up_to_date.Signal() self._calculation_thread = cothread.Spawn(self._recalculate_phys_data, callback)
def communicate(self, input=None): # @ReservedAssignment """Start a real OS thread to wait for process communication. """ if self._event is None: self._event = cothread.Event() threading.Thread(target=self._communicate_thread, args=(input,)).start() elif input is not None: raise RuntimeError("_Cothread_Popen: Communicate method already called") self._event.Wait() return (self._output[0], self._output[1], self._process.poll())
def communicate(self, input=None): """Start a real OS thread to wait for process communication. """ # To manintain compatibility with the standard library # the 'input' keyword argument is used which redefines # the builtin function. # pylint: disable=redefined-builtin if self._event is None: self._event = cothread.Event() threading.Thread(target=self._communicate_thread, args=(input, )).start() elif input is not None: raise RuntimeError("Popen: Communicate method already called") self._event.Wait() return (self._output[0], self._output[1], self._process.poll())
def rpc(self, name, value, request=None, timeout=5.0, throw=True): """Perform a Remote Procedure Call (RPC) operation :param str name: PV name string :param Value value: Arguments. Must be Value instance :param request: A :py:class:`p4p.Value` or string to qualify this request, or None to use a default. :param float timeout: Operation timeout in seconds :param bool throw: When true, operation error throws an exception. If False then the Exception is returned instead of the Value :returns: A Value or Exception. Subject to :py:ref:`unwrap`. >>> ctxt = Context('pva') >>> V = ctxt.rpc('pv:name:add', {'A':5, 'B'; 6}) >>> The provided value(s) will be automatically coerced to the target type. If this is not possible then an Exception is raised/returned. Unless the provided value is a dict, it is assumed to be a plain value and an attempt is made to store it in '.value' field. """ done = cothread.Event(auto_reset=False) def cb(value): assert not done, value if isinstance(value, (RemoteError, Disconnected, Cancelled)): done.SignalException(value) else: done.Signal(value) cb = partial(cothread.Callback, cb) op = super(Context, self).rpc(name, cb, value, request=request) _log.debug('rpc %s %s request=%s', name, LazyRepr(value), request) try: try: ret = done.Wait(timeout) except cothread.Timedout: ret = TimeoutError() if throw and isinstance(ret, Exception): raise ret finally: op.close() return ret
def __init__(self, latfactory, settings, chanprefix, data_dir, work_dir=None, **kws): if not isinstance(latfactory, FlameLatticeFactory): raise TypeError("VA: Invalid type for FlameLatticeFactory") self._latfactory = latfactory if not isinstance(settings, dict): raise TypeError("VA: Invalid type for accelerator Settings") self._settings = settings # for info PVs: status, mps, noise, etc. self._chanprefix = chanprefix self._data_dir = data_dir self._work_dir = work_dir self._epicsdb = [] self._csetmap = OrderedDict() self._elemmap = OrderedDict() self._fieldmap = OrderedDict() self._readfieldmap = OrderedDict() # noise self._noise = kws.get('noise', DEFAULT_NOISE_LEVEL) # rep-rate self._rate = kws.get('rate', DEFAULT_REP_RATE) # init beam conf, dict self._bsrc = None # suffix for noise/rate/cnt/status PVs self._pv_suffix = kws.get('pv_suffix', '') self._started = False self._continue = False self._rm_work_dir = False self._ioc_process = None self._ioc_logfile = None self._subscriptions = None self._wait_event = cothread.Event(False) self._ts_now = datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
def fill_buffer_one(pv, length, datatype=float, timeout=None): '''Performs a camonitor on pv to fill a buffer.''' count = [0] result = numpy.empty(length, dtype=datatype) done = cothread.Event() def on_update(value): result[count[0]] = value count[0] += 1 if count[0] >= length: done.Signal() subscription.close() subscription = catools.camonitor(pv, on_update, datatype=datatype) try: done.Wait(timeout) finally: subscription.close() return result
import cothread import random def worker(n, event): event.Wait() for i in range(25): print(f"{n}", end="") cothread.Sleep(random.random() * 0.5) # suspend cothread print() threads = [] event = cothread.Event(auto_reset=False) # auto_reset=True: let only one cothread see the signal # auto_reset=False: let all cothreads see the signal for n in range(1, 5): t = cothread.Spawn(worker, n, event) threads.append(t) delay = 10 print(f"main cothread waiting for {delay} seconds") cothread.Sleep(delay) event.Signal() for t in threads: t.Wait()
def __init__(self): self.evt = cothread.Event(auto_reset=False) self.conn = None
import time def signalViaCallback(n): return event.Signal(n) def producer(event, count): ''' this is a different thread from that running the cosumer code''' for n in "ABCDE": print('thread tick', n) try: # can't Signal from a different thread if n == "E": event.Signal(n) except AssertionError as e: print(f"{e}") # must use the following to talk across thread boundaries: cothread.Callback(signalViaCallback, n) time.sleep(0.5) event = cothread.Event() cothread.Spawn(consumer, event) thread = Thread(target=producer, args=(event, 5)) thread.start() cothread.Sleep(5)