Created
September 14, 2015 05:55
-
-
Save tiagocoutinho/76245dfbff80e3e184ff to your computer and use it in GitHub Desktop.
Gevent enabled TANGO device server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Demonstrate usage of TANGO DS with gevent. | |
Usage example: Go to jive and in the menu select Edit->Create server, then | |
fill in the fields: | |
Server (ServerName/Instance): GeventDS/test1 | |
Class: Gevent | |
Devices: test/gevent/1 | |
click Register server. | |
Download this file in a directory and on the command line do: | |
$ python GeventDS.py test1 | |
With jive do test device on *test/gevent/1*. Try reading the "bla" attribute. | |
You will see on the command line that the access was made on the `MainThread`. | |
Reading attribute **ble** will *not* be done on the *MainThread* due to the fact | |
that we forced green_mode=GreenMode.Synchronous | |
""" | |
import gevent | |
import logging | |
from PyTango import requires_pytango | |
from PyTango import Attr, DevState, CmdArgType, AttrWriteType, GreenMode | |
from PyTango.server import Device, DeviceMeta, attribute, command, run | |
requires_pytango('8.1.7', software_name="GeventDS") | |
class Gevent(Device): | |
__metaclass__ = DeviceMeta | |
def __init__(self, *args, **kwargs): | |
self._log = logging.getLogger(args[1]) | |
self.__bla = 55.6 | |
self._log.debug("__init__") | |
Device.__init__(self, *args, **kwargs) | |
def init_device(self): | |
self._log.info("init_device") | |
Device.init_device(self) | |
self.set_state(DevState.ON) | |
def delete_device(self): | |
self._log.info("delete_device") | |
@attribute | |
def bla(self): | |
"""bla attribute description""" | |
logging.debug("read bla") | |
return self.__bla | |
@bla.setter | |
def bla(self, bla): | |
logging.debug("write bla with %s", bla) | |
self.__bla = bla | |
@attribute(description="ble desc", green_mode=GreenMode.Synchronous) | |
def ble(self): | |
"""attribute with gevent disabled... just for fun""" | |
logging.debug("read ble") | |
return 100.01 | |
@command(dtype_in=float) | |
def nap(self, nap_time): | |
logging.info("started napping for %s", nap_time) | |
gevent.sleep(nap_time) | |
logging.info("finished napping for %s", nap_time) | |
def gworker(): | |
while True: | |
gevent.sleep(1) | |
logging.info("gevent loop") | |
def cb(): | |
logging.debug("Post init callback") | |
def main(): | |
logging.basicConfig(level=logging.DEBUG, | |
format="%(threadName)12s %(levelname)5s %(asctime)s %(name)s:%(message)s") | |
# spawn a greenlet here | |
greenlet = gevent.spawn(gworker) | |
# run tango server in gevent mode | |
run([Gevent], green_mode=GreenMode.Gevent, verbose=True, | |
post_init_callback=cb) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment