简体   繁体   中英

How can I use COM and multiprocessing at the same time in Python?

I'm trying to make two processes and make them communicate to each other. One of them gets values through a COM library using win32com, and the other just gets the values from the first process via a queue and prints them out. I think the code below has no problem, but it doesn't work(p2 process doesn't show values at all). If I just make the first process print queue values in the same process by

item = self.q.get()
print(item)

it shows values in the queue. So, I think putting values in the queue has no problem, and therefore, there could be some problems in exchanging values via the queue , using win32com

import win32com.client
import os
import multiprocessing as mp
from PyQt4.QtGui import QApplication
from datetime import datetime, timedelta

global q
q = mp.Queue()          # A queue is used to send values from p1 to p2                                                        

class RealTrEventHandler(object):          
    def __init__(self):
        self.q = q                                                            

    def OnReceiveRealData(self,szTrCode):
        date = datetime.utcnow() + timedelta(hours=3)
        type = self.GetFieldData("OutBlock", "cgubun")

        appending_line = date + ', ' + type

        self.q.put(appending_line)
        #item = self.q.get()                     # it prints values out if these are not comments
        #print(item)

def ticker():
    loop = QApplication([])
    global instXASession, instXAReal
    print('TICKER: ', os.getpid() )

    # When an event occurs, it calls RealTrEventHandler class
    instXAReal = win32com.client.DispatchWithEvents("XA_DataSet.XAReal", RealTrEventHandler) 
    instXAReal.LoadFromResFile("C:\\eBEST\\xingAPI\\Res\\OVC.res")
    instXAReal.SetFieldData("InBlock", "symbol", "CLX17")

    loop.exec_() 

class listener(mp.Process):           # What listener does is only to get values via the queue and prints them out 
    def __init__(self):
        mp.Process.__init__(self)
        self.q = q

    def run(self):
        print('CSM PID: ', os.getpid() )
        while True:
            item = self.q.get()
            print(item)

if __name__ == '__main__':
    loop = QApplication([])     

    print('MAIN: ', os.getpid() )
    p1 = mp.Process( target = ticker, args=() )
    p1.start()

    p2 = listener()
    p2.start()

    mp.freeze_support()

    loop.exec_()                

Could anyone give me some advice?

Have you tried to explicitly declare q as global in your listener and RealTrEventHandler class? Eg:

class listener(mp.Process):
    global q
    def __init__(self):
        mp.Process.__init__(self)
        self.q=q

Another way of passing variables between (at least) threads is using the buildins-module, but I'm not sure if multiprocessing is so much different.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM