From d3a298cd7c0381c682a09e5eef4544a2b3bf8612 Mon Sep 17 00:00:00 2001 From: Michael Davidsaver Date: Sat, 30 Mar 2013 15:03:11 -0400 Subject: [PATCH] add async example --- test.cmd | 4 +++ testApp/test1.db | 10 ++++++++ testApp/test2.py | 65 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 testApp/test2.py diff --git a/test.cmd b/test.cmd index e1b83f5..08ec763 100644 --- a/test.cmd +++ b/test.cmd @@ -8,6 +8,10 @@ devsup_registerRecordDeviceDriver(pdbbase) #evalPy "import devsup.hooks" #evalPy "devsup.hooks.debugHooks()" +evalPy "import test2" +evalPy "test2.addDrv('AAAA')" +evalPy "test2.addDrv('BBBB')" + dbLoadRecords("db/test1.db","P=md:") iocInit() diff --git a/testApp/test1.db b/testApp/test1.db index 66cce41..ecb729d 100644 --- a/testApp/test1.db +++ b/testApp/test1.db @@ -4,3 +4,13 @@ record(longin, "$(P)li:cnt") { field(INP , "@test1 hello world") field(SCAN, "1 second") } + +record(longin, "$(P)val:a") { + field(DTYP, "Python Device") + field(INP , "@test2 AAAA") +} + +record(longin, "$(P)val:b") { + field(DTYP, "Python Device") + field(INP , "@test2 AAAA") +} diff --git a/testApp/test2.py b/testApp/test2.py new file mode 100644 index 0000000..09b5a43 --- /dev/null +++ b/testApp/test2.py @@ -0,0 +1,65 @@ + +import threading, time +from devsup.hooks import addHook + +insts = {} + +class Driver(threading.Thread): + def __init__(self, name): + super(Driver,self).__init__() + self.name = name + self._lock = threading.Lock() + self._recs = set() + self._run = True + self._stop = threading.Event() + self.value = 0 + addHook('AfterIocRunning', self.start) + addHook('AtIocExit', self.stop) + + def stop(self): + print 'Stopping driver',self.name + with self._lock: + self._run = False + self._stop.wait() + print 'Finished with',self.value + + def addrec(self, rec): + with self._lock: + self._recs.add(rec) + def delrec(self, rec): + with self._lock: + self._recs.remove(rec) + + def run(self): + try: + while self._run: + time.sleep(1.0) + with self._lock: + val = self.value + self.value += 1 + for R in self._recs: + R.record.scan(sync=True, reason=val) + finally: + self._stop.set() + +def addDrv(name): + print 'Create driver',name + insts[name] = Driver(name) + +class Device(object): + def __init__(self, rec, drv): + self.driver, self.record = drv, rec + self.driver.addrec(self) + self.val = rec.field('VAL') + def detach(self, rec): + self.driver.delrec(self) + def process(self, rec, data): + if data is None: + print rec,'Someone processed me?' + else: + print rec,'update to',data + self.val.putval(data) + +def build(rec, args): + drv = insts[args] + return Device(rec, drv)