Polling VS Events: how to understand and use correctly?

Dear all,

I have been into Tango for a couple of months and am in the middle of building a real system now using mostly taurus & pytango.

Though I have read the documentations of tango, pytango and taurus, I am still a bit confused on the co-existence of polling and events. I read that since events are much more efficient they are recommended over polling, but I also notice that polling are implemented in most device server sample code and tango clients and forum snippets etc. I am so far so good with polling but I would like to understand events and use them in place of polling as much as possible to optimize the system.

So any insights or advice into this matter would be greatly appreciated.

My understanding:
1. Polling
In this model, device servers are polled by tango every “interval” (e.g. 1 second, this can be changed). This can create bottle necks. I notice that my synoptic viewer (jdraw built in synoptic viewer) can be laggy when there are many devices involved. So polling is good if used wisely and moderately.

2. Events
In this model, device servers push events out, and clients can get these events by registering some listeners. I wrote some test client to test and it’s all good.

My questions:
1. Is it possible to not use polling at all and only use events to achieve max efficiency?

2. In device servers, where to push events? Say in my device, I have a

read_voltage

command that reads the value of the voltage attribute. In this function, I of course will need to write I/O code to get the voltage from the real hardware. I see that if we push the changed value of voltage in this function, the push may not be triggered at all, since there is no polling to trigger the read_voltage command (?) Also for that reason, pushing the value out in this function would make no point at all since the function is polled regularly anyway, is that correct?

The other way I have done is that to implement events, I just leave the read_voltage function blank, and create an “voltage_update” thread inside the device server. In this thread, the voltage value is read regularly and push out. This is fine (though I have no idea if this is the proper way or not), but I notice that when the number of devices in this class are numerous, there will be so many threads created, and in my experiment that can lead to Segmentation Fault when starting the device server. Also creating so many threads just for this reason is not very healthy IMHO.

3. If (1) is possible, how to disable polling, or maximize the use of events? What are the use cases of polling vs events? For my tango clients that I create from my own pytango code, of course I can register to events the client is interested in only. But in this case, how clients such as jive, synoptic viewer, or Taurus Gui widgets are supposed to communicate with the device servers?

I am sorry if the questions are not totally clear since I am still confused.