从 python 调用 pj_thread_register

我正在尝试在 python 中通过 pjsua2 使用 pjsip。我有线程问题。


import pjsua2 as pj

import threading


# pjsua2 test function

def pjsua2_test():

  # Create and initialize the library

  ep = pj.Endpoint()

  ep.libCreate()


if __name__ == "__main__":

  pjsuadaemonthread = threading.Thread(name='PJSUA',

                      target=pjsua2_test)

  pjsuadaemonthread.start()

  ep = pj.Endpoint()

  ep.libHandleEvents(10)

我收到此错误:


python3: ../src/pj/os_core_unix.c:692: pj_thread_this: Assertion `!"Calling pjlib from unknown/external thread. You must " "register external threads with pj_thread_register() " "before calling any pjlib functions."' failed.

如何从 python 调用 pj_thread_register()?


暮色呼如
浏览 239回答 1
1回答

LEATH

这是 pj_thread_register() 的语法:ep.libRegisterThread('PJSUA')不幸的是,即使您调用 libRegisterThread,它似乎也与 python 线程不兼容。我认为这是因为 pjsip 内部使用 pj_thread_t 类型的线程,而 python 线程不是 pj_thread_t 类型。有关更多信息,请查看 pjsip 源代码中的 Endpoint::libRegisterThread。但是,如果您只想在不同的线程上运行 pjsip,下面的代码可能会有所帮助。它在名为“PJSUA-THREAD”的后台线程上运行 pjsip。关键是像我在下面做的那样在不同的线程上调用 libCreate()。'''Tests running PJSUA2 in a different thread'''import loggingimport threadingimport pjsua2date_fmt = '%Y-%m-%d,%H:%M:%S'log_format = "%(levelname)s %(asctime)s.%(msecs)03d %(threadName)s %(name)s.%(funcName)s %(message)s"logging.basicConfig(format=log_format, datefmt=date_fmt, level=logging.DEBUG)_logger = logging.getLogger(__name__)THREAD_NAME = "PJSUA-THREAD"def main():&nbsp; &nbsp; _logger.debug("entered main()")&nbsp; &nbsp; pjsuadaemonthread = threading.Thread(name=THREAD_NAME, target=pjsua2_test)&nbsp; &nbsp; pjsuadaemonthread.start()&nbsp; &nbsp; _logger.debug("thread started")&nbsp; &nbsp; pjsuadaemonthread.join()# pjsua2 test functiondef pjsua2_test():&nbsp; &nbsp; # NOTE: PjSip will initialize and run on PJSUA-THREAD, see logs&nbsp; &nbsp; ep = pjsua2.Endpoint()&nbsp; &nbsp; ep.libCreate()&nbsp; &nbsp; _logger.debug("after libCreate()")&nbsp; &nbsp; ep_cfg = pjsua2.EpConfig()&nbsp; &nbsp; ep_cfg.uaConfig.mainThreadOnly = False&nbsp; &nbsp; ep_cfg.uaConfig.threadCnt = 1&nbsp; &nbsp; ep.libInit(ep_cfg)&nbsp; &nbsp; _logger.debug("after libInit(ep_cfg)")&nbsp; &nbsp; ep.libStart()&nbsp; &nbsp; _logger.debug("after libStart()")&nbsp; &nbsp; iter_count = 0&nbsp; &nbsp; event_count = 0&nbsp; &nbsp; while iter_count < 10:&nbsp; &nbsp; &nbsp; &nbsp; event_count += ep.libHandleEvents(10)&nbsp; &nbsp; &nbsp; &nbsp; _logger.debug("iter_count %d (%d events processed)", iter_count, event_count)&nbsp; &nbsp; &nbsp; &nbsp; iter_count += 1&nbsp; &nbsp; ep.libDestroy()&nbsp; &nbsp; _logger.debug("after ep.libDestroy()")if __name__ == "__main__":&nbsp; &nbsp; main()&nbsp; &nbsp; _logger.debug("after main()")这是日志输出。您可以看到 pjsip 正在线程 PJSUA-THREAD 上运行。DEBUG 2020-05-31,14:49:43.259 MainThread __main__.main entered main()14:49:43.261&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;os_core_unix.c !pjlib 2.8 for POSIX initialized14:49:43.261&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sip_endpoint.c&nbsp; .Creating endpoint instance...14:49:43.262&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pjlib&nbsp; .select() I/O Queue created (0x7fd530013550)14:49:43.262&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sip_endpoint.c&nbsp; .Module "mod-msg-print" registered14:49:43.262&nbsp; &nbsp; &nbsp; &nbsp; sip_transport.c&nbsp; .Transport manager created.14:49:43.262&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pjsua_core.c&nbsp; .PJSUA state changed: NULL --> CREATEDDEBUG 2020-05-31,14:49:43.262 PJSUA-THREAD __main__.pjsua2_test after libCreate()14:49:43.262&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sip_endpoint.c&nbsp; .Module "mod-pjsua-log" registered14:49:43.263&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sip_endpoint.c&nbsp; .Module "mod-tsx-layer" registered14:49:43.263&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sip_endpoint.c&nbsp; .Module "mod-stateful-util" registered14:49:43.263&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sip_endpoint.c&nbsp; .Module "mod-ua" registered14:49:43.263&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sip_endpoint.c&nbsp; .Module "mod-100rel" registered14:49:43.263&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sip_endpoint.c&nbsp; .Module "mod-pjsua" registered14:49:43.263&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sip_endpoint.c&nbsp; .Module "mod-invite" registered14:49:43.496&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;alsa_dev.c&nbsp; ..ALSA driver found 32 devices14:49:43.496&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;alsa_dev.c&nbsp; ..ALSA initialized14:49:43.496&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pjlib&nbsp; ..select() I/O Queue created (0x7fd53009cf08)14:49:43.499&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sip_endpoint.c&nbsp; .Module "mod-evsub" registered14:49:43.499&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sip_endpoint.c&nbsp; .Module "mod-presence" registered14:49:43.499&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sip_endpoint.c&nbsp; .Module "mod-mwi" registered14:49:43.499&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sip_endpoint.c&nbsp; .Module "mod-refer" registered14:49:43.499&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sip_endpoint.c&nbsp; .Module "mod-pjsua-pres" registered14:49:43.499&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sip_endpoint.c&nbsp; .Module "mod-pjsua-im" registered14:49:43.499&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sip_endpoint.c&nbsp; .Module "mod-pjsua-options" registered14:49:43.499&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pjsua_core.c&nbsp; .1 SIP worker threads created14:49:43.499&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pjsua_core.c&nbsp; .pjsua version 2.8 for Linux-5.3.0.53/x86_64/glibc-2.27 initialized14:49:43.499&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pjsua_core.c&nbsp; .PJSUA state changed: CREATED --> INITDEBUG 2020-05-31,14:49:43.262 MainThread __main__.main thread startedDEBUG 2020-05-31,14:49:43.499 PJSUA-THREAD __main__.pjsua2_test after libInit(ep_cfg)14:49:43.499&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pjsua_core.c&nbsp; PJSUA state changed: INIT --> STARTINGDEBUG 2020-05-31,14:49:43.500 PJSUA-THREAD __main__.pjsua2_test after libStart()14:49:43.500&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sip_endpoint.c&nbsp; .Module "mod-unsolicited-mwi" registered14:49:43.500&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pjsua_core.c&nbsp; .PJSUA state changed: STARTING --> RUNNINGDEBUG 2020-05-31,14:49:43.510 PJSUA-THREAD __main__.pjsua2_test iter_count 0 (0 events processed)DEBUG 2020-05-31,14:49:43.520 PJSUA-THREAD __main__.pjsua2_test iter_count 1 (0 events processed)DEBUG 2020-05-31,14:49:43.531 PJSUA-THREAD __main__.pjsua2_test iter_count 2 (0 events processed)DEBUG 2020-05-31,14:49:43.541 PJSUA-THREAD __main__.pjsua2_test iter_count 3 (0 events processed)DEBUG 2020-05-31,14:49:43.551 PJSUA-THREAD __main__.pjsua2_test iter_count 4 (0 events processed)DEBUG 2020-05-31,14:49:43.562 PJSUA-THREAD __main__.pjsua2_test iter_count 5 (0 events processed)DEBUG 2020-05-31,14:49:43.572 PJSUA-THREAD __main__.pjsua2_test iter_count 6 (0 events processed)DEBUG 2020-05-31,14:49:43.582 PJSUA-THREAD __main__.pjsua2_test iter_count 7 (0 events processed)DEBUG 2020-05-31,14:49:43.593 PJSUA-THREAD __main__.pjsua2_test iter_count 8 (0 events processed)DEBUG 2020-05-31,14:49:43.603 PJSUA-THREAD __main__.pjsua2_test iter_count 9 (0 events processed)14:49:43.604&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pjsua_core.c&nbsp; Shutting down, flags=0...14:49:43.604&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pjsua_core.c&nbsp; PJSUA state changed: RUNNING --> CLOSING14:49:43.610&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pjsua_call.c&nbsp; .Hangup all calls..14:49:43.610&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pjsua_media.c&nbsp; .Call 0: deinitializing media..14:49:43.610&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pjsua_media.c&nbsp; .Call 1: deinitializing media..14:49:43.610&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pjsua_media.c&nbsp; .Call 2: deinitializing media..14:49:43.610&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pjsua_media.c&nbsp; .Call 3: deinitializing media..14:49:43.610&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pjsua_pres.c&nbsp; .Shutting down presence..14:49:44.610&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pjsua_core.c&nbsp; .Destroying...14:49:44.610&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pjsua_media.c&nbsp; .Shutting down media..14:49:44.997&nbsp; &nbsp; &nbsp; sip_transaction.c&nbsp; .Stopping transaction layer module14:49:44.997&nbsp; &nbsp; &nbsp; sip_transaction.c&nbsp; .Stopped transaction layer module14:49:44.997&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sip_endpoint.c&nbsp; .Module "mod-unsolicited-mwi" unregistered14:49:44.997&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sip_endpoint.c&nbsp; .Module "mod-pjsua-options" unregistered14:49:44.997&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sip_endpoint.c&nbsp; .Module "mod-pjsua-im" unregistered14:49:44.997&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sip_endpoint.c&nbsp; .Module "mod-pjsua-pres" unregistered14:49:44.997&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sip_endpoint.c&nbsp; .Module "mod-pjsua" unregistered14:49:44.997&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sip_endpoint.c&nbsp; .Module "mod-stateful-util" unregistered14:49:44.997&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sip_endpoint.c&nbsp; .Module "mod-refer" unregistered14:49:44.997&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sip_endpoint.c&nbsp; .Module "mod-mwi" unregistered14:49:44.997&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sip_endpoint.c&nbsp; .Module "mod-presence" unregistered14:49:44.997&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sip_endpoint.c&nbsp; .Module "mod-evsub" unregistered14:49:44.997&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sip_endpoint.c&nbsp; .Module "mod-invite" unregistered14:49:44.997&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sip_endpoint.c&nbsp; .Module "mod-100rel" unregistered14:49:44.997&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sip_endpoint.c&nbsp; .Module "mod-ua" unregistered14:49:44.997&nbsp; &nbsp; &nbsp; sip_transaction.c&nbsp; .Transaction layer module destroyed14:49:44.997&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sip_endpoint.c&nbsp; .Module "mod-tsx-layer" unregistered14:49:44.997&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sip_endpoint.c&nbsp; .Module "mod-msg-print" unregistered14:49:44.997&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sip_endpoint.c&nbsp; .Module "mod-pjsua-log" unregistered14:49:44.997&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sip_endpoint.c&nbsp; .Endpoint 0x7fd5300087b8 destroyed14:49:44.997&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pjsua_core.c&nbsp; .PJSUA state changed: CLOSING --> NULL14:49:44.997&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pjsua_core.c&nbsp; .PJSUA destroyed...DEBUG 2020-05-31,14:49:44.997 PJSUA-THREAD __main__.pjsua2_test after ep.libDestroy()DEBUG 2020-05-31,14:49:45.000 MainThread __main__.<module> after main()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python