我在主线程中尝试了该示例this,但一切正常,但是当我在多线程中作为第一个线程启动它时,无法启动其他线程。
loop.call_soon_threadsafe
,没有帮助 def _callback_func(self, snmpEngine, stateReference, contextEngineId, contextName,
varBinds, cbCtx):
execContext = snmpEngine.observer.getExecutionContext(
'rfc3412.receiveMessage:request')
source = (execContext['transportAddress'][0]) # source
trap_oid = varBinds[1][1]
value = varBinds[2][1]
trap=[]
trap.append(source)
trap.append(trap_oid)
trap.append(value)
self.q.put(trap)
self.logger.warning(f"[SNMPTRAP] Received trap:{source}, {trap_oid}, {value}")
def _receiver(self):
try:
# Register SNMP Application at the SNMP engine
ntfrcv.NotificationReceiver(self.snmpEngine, self._callback_func)
self.loop.run_forever()
except Exception as e:
self.logger.exception(str(e))
def _configure(self):
return self
def start(self):
self.worker = Thread(target=self._receiver)
self._configure()
try:
self.logger.info("Starting thread 'trap receiver'...")
self.worker.start()
if self.worker.is_alive():
self.logger.info("Thread 'trap_receiver' started")
while True:
data = self.q.get()
self.logger.debug(f"Returned trap {data}")
return data
else:
self.logger.error("Couldn't start thread 'trap receiver'")
except Exception as e:
self.logger.error(f"An exception occured while starting thread 'trap receiver' {str(e)}")