简体   繁体   English

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

[英]Threading Condition Acquire lock is not actually acquiring the lock

I am trying to create a ZMQ Subscriber that writes to a CSV every certain amount of time (The time doesn't matter as much) 我正在尝试创建一个ZMQ订阅服务器,该订阅服务器每隔一定的时间写入CSV文件(时间无关紧要)

My problem im having is in my write class. 我遇到的问题是在我的写作课上。 When I'm run the c.acquire to acquire the lock it doesnt do anything and the loop just freezes 当我运行c.acquire来获取锁时,它什么也不做,并且循环只是冻结了

The c.acquire in my ZMQ_Thread class is actually working so Im not sure what I'm doing wrong. ZMQ_Thread类中的c.acquire实际上正在工作,所以我不确定我在做什么错。

Any pointers or tips would be amazing. 任何指示或技巧将是惊人的。 Thanks in advance. 提前致谢。

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()

The answer to this, is that I wasn't calling c.wait(). 答案是,我没有调用c.wait()。 this is the code snip from my code that I added c.wait() where c.notify() was. 这是我在c.notify()所在的位置添加了c.wait()的代码片段。 It waits for the next process to acquire the lock then starts the write_csv class. 它等待下一个进程获取锁,然后启动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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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