The XDAClient module has the disadvantage that operations can only be handled sequentially. When certain OPC operations take significantly longer than others, it is a better solution to execute the requests in parallel. The Twisted framework introduces an event-based mechanism that is utilized by the the TWXDAClient, so that concurrent server requests can be made13. However, this Twisted-based client class is more complex than its simpler alternative. More information about the Twisted framework and its underlying concepts can be found in [FET06].
Listing 8 implements the same functionality as listing 7 but executes the three OPC operations concurrently:
In line 3, certain Twisted modules are imported. Line 24-26 show how an OPC operation is done in ``Twisted style'': first, the method twGetStatus is called that returns a deferred. Then two functions are attached to this deferred, namely a ``callback'' method, which prints the results of the OPC operation and an ``errback'' method, which is executed when an error (failure) is returned.
In line 37, all deferreds are initialized, therefore the Twisted reactor is started, which triggers all deferreds. This way, all OPC operations are started and when the requested data is returned, the appropriate callback/errback methods are called.
As the execution order of the attached callback methods cannot be predicted, a global variable OPERATIONS is defined, which is used by the function print_options to stop the Twisted reactor when all pending server requests have finished.