Python多线程-connection.wait()上的Rawlink错误

时间:2018-10-24 10:48:40

标签: python python-3.x multithreading connection signalr

我正在尝试使用python对Singal R连接进行多线程,但出现连接Rawlink错误,表明连接有效,我需要等待1秒才能从客户端接收消息,我也在使用执行线程“同时”的障碍。

这是我的代码

with Session() as session:
    global connection
    connection = Connection("http://sampleSINGALRURL/signalr", session)
    presenceservice = connection.register_hub('ClientRegistration')
    presenceservice1 = connection.register_hub('PresenceClientHub')
    connection.start()
    presenceservice.server.invoke('IdentifyClient', devideIdentity, softwareVersion, IpAddress,
                                  machineName, DeviceType, patientAdmissionGuid, patientID, pairingId)
    presenceservice1.client.on('StaffPresenceNotified', self.get_data1)
    connection.wait(1)

然后是我的线程函数

def get_clients(self):
    global barrier
    self.connect_to_database1()
    barrier.wait()
    self.get_message_from_client1()
    self.print_data1()

def send_messages(self):
    global MessageNumber
    global machineName
    global staffName
    global request
    machineName = final_result[MessageNumber][0]

    staffName = staff_results[MessageNumber][0]
    MessageNumber += 1
    barrier.wait()
    request = requests.post(
        "http://sampleurl/api/sample")
    return request
def print_response(self):
    global request
    timestamp = datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
    logging.info("Message sent at "  + "Time : " + timestamp + " " + machineName)


def Spin_Clients(self, NumMessages):
    for i in range(10):
        self.client_list.append(Thread(target=self.send_messages))
        self.client_list[i].start()
        self.print_response()
    sleep(2)
    for i in range(10):
        self.Message_List.append(Thread(target=self.get_clients))
        self.Message_List[i].start()
    for thread in self.client_list:
        thread.join()
    for thread in self.Message_List:
        thread.join()

错误日志

All threads have finished    
11:41:37.243    
Exception in thread Thread-13: Traceback (most recent call last):   File "c:\users\appdata\local\programs\python\python37\lib\threading.py", line 917, in _bootstrap_inner
        self.run()   
File "c:\users\appdata\local\programs\python\python37\lib\threading.py", line 865, in run
        self._target(*self._args, **self._kwargs)   
File "C:\Data\LoadTest.py", line 103, in get_clients
        self.get_message_from_client1()   
File "C:\Data\LoadTest.py", line 89, in get_message_from_client1
        connection.wait(1)  
File "c:\users\appdata\local\programs\python\python37\lib\site-packages\signalr\_connection.py", line 60, in wait
            gevent.joinall([self.__greenlet], timeout)   
 File "src\gevent\greenlet.py", line 849, in gevent._greenlet.joinall  
 File "src\gevent\greenlet.py", line 859, in gevent._greenlet.joinall   
 File "src\gevent\_hub_primitives.py", line 198, in gevent.__hub_primitives.wait_on_objects   File "src\gevent\_hub_primitives.py", line 235, in gevent.__hub_primitives.wait_on_objects   
 File "src\gevent\_hub_primitives.py", line 125, in gevent.__hub_primitives._WaitIterator.__iter__ AttributeError: 'NoneType' object has no attribute 'rawlink'

我也尝试使用锁,但是结果相同。

有什么主意吗?

0 个答案:

没有答案