线程条件“获取锁”实际上并未获取该锁

时间:2018-09-26 19:23:38

标签: python python-3.x multithreading pandas zeromq

我正在尝试创建一个ZMQ订阅服务器,该订阅服务器每隔一定的时间(不重要的时间)写入CSV

我遇到的问题是在我的写课中。当我运行c.acquire来获取锁时,它什么也不做,并且循环只是冻结

我的ZMQ_Thread类中的c.acquire实际上正在工作,所以我不确定我在做什么错。

任何指针或技巧都将是惊人的。预先感谢。

import zmq
import pandas as pd
import time
import threading


c = threading.Condition()
df = pd.DataFrame()
s = 0
m = 0
h = 0
d = 0

def counter():
    global h,s,m,d
    while True:
        s += 1
        print("Second:{}".format(s))
        if s >=60:
            m +=1
            s = 0
        if m >= 60:
            h += 1
            m = 0
        if h >= 24:
            d += 1
            h = 0        
        time.sleep(1)

class write(threading.Thread):
    def __init__(self,name):
        threading.Thread.__init__(self)
        self.name = name
    def run(self):
        global df
        while True:            
            print('sleeping')            
            time.sleep(12)
            c.acquire()
            print('acquired')
            print(df)                 
            with open("FILE", 'a') as f:
                    df.to_csv(f, encoding = 'utf-8', index = False, header = False)
            c.release()
            c.notify()
            z = zmq_thread()            
            z.run()        

class zmq_thread(threading.Thread):
    def __init__(self,name):
        threading.Thread.__init__(self)
        self.name = name
    def run(self):  
        global df
        print('DF Created')
        context = zmq.Context()
        socket = context.socket(zmq.SUB)      
        socket.connect("tcp://localhost:#####")
        socket.setsockopt_string(zmq.SUBSCRIBE, 'TOPIC') 
        print('socket connected')

        count = 0
        while True: 
            c.acquire()
            try:
                count +=1
                if count == 10:
                    print('ZMQ Break') 
                    c.notify()
                    c.release()
                    print('Lock released')
                    return df                
                message = socket.recv_string()
                message2 = socket.recv_string()        
                if message == 'TOPIC':
                    message2_split = message2.split(",")
                    message4 = pd.Series(message2_split)
                    df = df.append(message4, ignore_index=True)               
                    print('ZMQ Running')

            except KeyboardInterrupt:
                break              



counter = threading.Thread(target = counter)
write_csv1 = write("Write_csv")
zmq_loop = zmq_thread('Start_ZMQ')    

counter.start()
write_csv1.start()    
zmq_loop.start()

1 个答案:

答案 0 :(得分:0)

对此的答案是,我没有调用c.wait()。这是我在c.notify()所在的位置添加了c.wait()的代码片段。它等待下一个进程获取锁,然后启动write_csv类。

while True: 
            c.acquire()
            try:
                count +=1
                if count == 10:
                    print('ZMQ Break') 
                    c.wait()                                      
                message = socket.recv_string()
                message2 = socket.recv_string()        
                if message == 'TOPIC':
                    message2_split = message2.split(",")
                    message4 = pd.Series(message2_split)
                    df = df.append(message4, ignore_index=True)               
                    print('ZMQ Running')