使用Barrier Python时程序卡住

时间:2018-10-19 07:30:46

标签: python multithreading barrier

我需要同步线程以同时执行它们,这一切都没有障碍,但是线程在avg上执行。每秒2个,并且我需要至少50-100个线程同时执行。

当我在代码中使用Barrier时,我的程序像死锁一样卡住了。

def Spin_Clients(self):
    def get_clients():
        self.connect_to_database1()
        self.get_message_from_client1()
    client_list = []
    barrier = Barrier(10)
    def send_messages():
        global MessageNumber
        global machineName
        global staffName
        timestamp = datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
        logging.info("Thread {} working on something".format(threading.current_thread()))

        logging.info("Thread {} working on something1".format(threading.current_thread()))

        machineName = final_result[MessageNumber][0]
        logging.info("Thread {} working on something2".format(threading.current_thread()))

        staffName = staff_results[MessageNumber][0]
        logging.info("Thread {} working on something3".format(threading.current_thread()))
        MessageNumber += 1
        logging.info("Thread {} working on something4".format(threading.current_thread()))

        r = requests.post(
            "http://sampleurl"+ staffName + "&locationId=" + machineName)
        logging.info("Thread {} working on something5".format(threading.current_thread()))

        barrier.wait(timeout=5)
        logging.info("Response Code Should be 202: " + str(r.status_code) + " Time: " + timestamp)

        logging.info("Thread {} working on something6".format(threading.current_thread()))

    for i in range(10):
        t = Thread(target=send_messages(), args=(i,))
        t.start()
        logging.info("Message Sent to: " + staffName + " " + machineName)
    for thread in client_list:
        thread.join()

我需要每秒发送50-100个发帖请求,如何使用python线程同步来实现呢?

我没有任何错误,我需要终止会话以在运行时关闭问题。

根据评论,在添加超时并记录每行之后,这就是我得到的结果

INFO    Thread <_MainThread(MainThread, started 68480)> working on something    
09:19:53.929    INFO    Thread <_MainThread(MainThread, started 68480)> working on something1   
09:19:53.929    INFO    Thread <_MainThread(MainThread, started 68480)> working on something2   
09:19:53.929    INFO    Thread <_MainThread(MainThread, started 68480)> working on something3   
09:19:53.929    INFO    Thread <_MainThread(MainThread, started 68480)> working on something4   
09:19:53.963    INFO    Thread <_MainThread(MainThread, started 68480)> working on something5   
09:19:58.964    FAIL    BrokenBarrierError

更新2:

现在我只是试图运行一个简单的打印线程,但我仍然得到BrokenBarrierError

def Spin_Clients(self, NumMessages):


    def get_clients():
        self.connect_to_database1()
        self.get_message_from_client1()
    client_list = []
    barrier = Barrier(10)

    def send_messages():

        logging.info("Thread {} Working on something".format(threading.current_thread()))
        logging.info("Thread {} I have Reached the barrier".format(threading.current_thread()))
        barrier.wait(timeout=10)
    for i in range(10):
        client_list.append(Thread(target=send_messages(), args=(i,)))
        client_list[].start()

    for thread in client_list:
        thread.join()
    print("All threads have finished")

0 个答案:

没有答案