Is it possible to use Asyncio with win32com in Pyt

2019-06-13 19:04发布

问题:

My python program receives data via Windows COM objects record it into an hdf5 file. In order to use the COM objects, I have used win32com.client.DispatchWithEvents. The code below shows the simplified structure of my program.

class Handler_realTime(object):  
    def __init__(self):
        pass

    def OnReceiveRealData(self, eventTime, eventData01, eventData02, eventData03):
        m.target_table.append( np.array([( eventTime, eventData01, eventData02, eventData03 )] )
        m.file.flush()        # <--- being delayed due to IO processing!



class MainClass(object):
    def __init__(self):
        self.file = tb.open_file('hdf5File.h5', 'a')
        self.target_table = self.file.root.realTime

        self.realReceving = win32com.client.DispatchWithEvents("Session.RealTime",Handler_realTime)
        # calls Handler_realTime whenever new data arrives

m = MainClass()

It works fine, but I have recently felt the low-performance of the program, due to its frequent flush() and excessive calls. Then, I thought that Asyncio might improve the performance, by randomly flushing the file and processing other tasks at the same time.

I have looked up some books on Asyncio in Python, but I couldn't find any examples that use classes instead of funcsions(async def). In other words, I don't know if it's possible to use functions(not classes) with DispatchWithEvents or if it's possible to adopt Asyncio in this situation.