From d01aa71b31b3a39fd3301d0cca0292fad3d7e637 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Tue, 17 Apr 2012 12:22:22 -0400 Subject: [PATCH] Add `block_while_running` option to `_ReadWriteThread`. This way users don't need to understand the following distinctions: * written to driver vs. output from card. * `SDF_BUSY` vs. `SDF_RUNNING`. The `subdevice.cancel` call in `_ReadWriteThread.block` is because the `RUNNING` flag is cleared after output completes, but the `BUSY` flag is not. This may be a bug in Comedi. I've asked on the mailing list, but haven't heard back yet. --- pycomedi/utility.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/pycomedi/utility.py b/pycomedi/utility.py index 890c845..e3f0b09 100644 --- a/pycomedi/utility.py +++ b/pycomedi/utility.py @@ -81,12 +81,14 @@ def _builtin_array(array): class _ReadWriteThread (_threading.Thread): "Base class for all reader/writer threads" - def __init__(self, subdevice, buffer, name=None): + def __init__(self, subdevice, buffer, name=None, + block_while_running=False): if name == None: name = '<%s subdevice %d>' % ( self.__class__.__name__, subdevice.index) self.subdevice = subdevice self.buffer = buffer + self.block_while_running = block_while_running self._setup_buffer() super(_ReadWriteThread, self).__init__(name=name) @@ -103,6 +105,11 @@ class _ReadWriteThread (_threading.Thread): """ return self.subdevice.device.file + def block(self): + while self.subdevice.get_flags().running: + _time.sleep(0) + self.subdevice.cancel() # become unbusy + class Reader (_ReadWriteThread): """`read()`-based reader @@ -180,12 +187,8 @@ class Reader (_ReadWriteThread): shape=self.buffer.shape, dtype=self.buffer.dtype, buffer=buf) self.buffer[:] = a - #_LOG.critical('ai running? %s' % self.subdevice.get_flags().running) - #while self.subdevice.get_flags().running: - ##_LOG.critical('ai running? %s' % self.subdevice.get_flags().running) - # _time.sleep(0) - #_LOG.critical('ai running? %s' % self.subdevice.get_flags().running) - #_time.sleep(1) + if self.block_while_running: + self.block() class Writer (_ReadWriteThread): @@ -266,12 +269,8 @@ class Writer (_ReadWriteThread): f = self._file() remaining_buffer.tofile(f) f.flush() - #_LOG.critical('ao running? %s' % self.subdevice.get_flags().running) - #while self.subdevice.get_flags().running: - ##_LOG.critical('ao running? %s' % self.subdevice.get_flags().running) - #_time.sleep(0) - #_LOG.critical('ao running? %s' % self.subdevice.get_flags().running) - #_time.sleep(1) + if self.block_while_running: + self.block() class _MMapReadWriteThread (_ReadWriteThread): @@ -325,6 +324,8 @@ class _MMapReadWriteThread (_ReadWriteThread): remaining -= action else: _time.sleep(sleep_time) + if self.block_while_running: + self.block() def _act(self, mmap, mmap_offset, buffer_offset, remaining, mmap_size, action_bytes=None, builtin_array=None): -- 2.26.2