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(): _logger.debug("entered main()") pjsuadaemonthread = threading.Thread(name=THREAD_NAME, target=pjsua2_test) pjsuadaemonthread.start() _logger.debug("thread started") pjsuadaemonthread.join()# pjsua2 test functiondef pjsua2_test(): # NOTE: PjSip will initialize and run on PJSUA-THREAD, see logs ep = pjsua2.Endpoint() ep.libCreate() _logger.debug("after libCreate()") ep_cfg = pjsua2.EpConfig() ep_cfg.uaConfig.mainThreadOnly = False ep_cfg.uaConfig.threadCnt = 1 ep.libInit(ep_cfg) _logger.debug("after libInit(ep_cfg)") ep.libStart() _logger.debug("after libStart()") iter_count = 0 event_count = 0 while iter_count < 10: event_count += ep.libHandleEvents(10) _logger.debug("iter_count %d (%d events processed)", iter_count, event_count) iter_count += 1 ep.libDestroy() _logger.debug("after ep.libDestroy()")if __name__ == "__main__": main() _logger.debug("after main()")这是日志输出。您可以看到 pjsip 正在线程 PJSUA-THREAD 上运行。DEBUG 2020-05-31,14:49:43.259 MainThread __main__.main entered main()14:49:43.261 os_core_unix.c !pjlib 2.8 for POSIX initialized14:49:43.261 sip_endpoint.c .Creating endpoint instance...14:49:43.262 pjlib .select() I/O Queue created (0x7fd530013550)14:49:43.262 sip_endpoint.c .Module "mod-msg-print" registered14:49:43.262 sip_transport.c .Transport manager created.14:49:43.262 pjsua_core.c .PJSUA state changed: NULL --> CREATEDDEBUG 2020-05-31,14:49:43.262 PJSUA-THREAD __main__.pjsua2_test after libCreate()14:49:43.262 sip_endpoint.c .Module "mod-pjsua-log" registered14:49:43.263 sip_endpoint.c .Module "mod-tsx-layer" registered14:49:43.263 sip_endpoint.c .Module "mod-stateful-util" registered14:49:43.263 sip_endpoint.c .Module "mod-ua" registered14:49:43.263 sip_endpoint.c .Module "mod-100rel" registered14:49:43.263 sip_endpoint.c .Module "mod-pjsua" registered14:49:43.263 sip_endpoint.c .Module "mod-invite" registered14:49:43.496 alsa_dev.c ..ALSA driver found 32 devices14:49:43.496 alsa_dev.c ..ALSA initialized14:49:43.496 pjlib ..select() I/O Queue created (0x7fd53009cf08)14:49:43.499 sip_endpoint.c .Module "mod-evsub" registered14:49:43.499 sip_endpoint.c .Module "mod-presence" registered14:49:43.499 sip_endpoint.c .Module "mod-mwi" registered14:49:43.499 sip_endpoint.c .Module "mod-refer" registered14:49:43.499 sip_endpoint.c .Module "mod-pjsua-pres" registered14:49:43.499 sip_endpoint.c .Module "mod-pjsua-im" registered14:49:43.499 sip_endpoint.c .Module "mod-pjsua-options" registered14:49:43.499 pjsua_core.c .1 SIP worker threads created14:49:43.499 pjsua_core.c .pjsua version 2.8 for Linux-5.3.0.53/x86_64/glibc-2.27 initialized14:49:43.499 pjsua_core.c .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 pjsua_core.c PJSUA state changed: INIT --> STARTINGDEBUG 2020-05-31,14:49:43.500 PJSUA-THREAD __main__.pjsua2_test after libStart()14:49:43.500 sip_endpoint.c .Module "mod-unsolicited-mwi" registered14:49:43.500 pjsua_core.c .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 pjsua_core.c Shutting down, flags=0...14:49:43.604 pjsua_core.c PJSUA state changed: RUNNING --> CLOSING14:49:43.610 pjsua_call.c .Hangup all calls..14:49:43.610 pjsua_media.c .Call 0: deinitializing media..14:49:43.610 pjsua_media.c .Call 1: deinitializing media..14:49:43.610 pjsua_media.c .Call 2: deinitializing media..14:49:43.610 pjsua_media.c .Call 3: deinitializing media..14:49:43.610 pjsua_pres.c .Shutting down presence..14:49:44.610 pjsua_core.c .Destroying...14:49:44.610 pjsua_media.c .Shutting down media..14:49:44.997 sip_transaction.c .Stopping transaction layer module14:49:44.997 sip_transaction.c .Stopped transaction layer module14:49:44.997 sip_endpoint.c .Module "mod-unsolicited-mwi" unregistered14:49:44.997 sip_endpoint.c .Module "mod-pjsua-options" unregistered14:49:44.997 sip_endpoint.c .Module "mod-pjsua-im" unregistered14:49:44.997 sip_endpoint.c .Module "mod-pjsua-pres" unregistered14:49:44.997 sip_endpoint.c .Module "mod-pjsua" unregistered14:49:44.997 sip_endpoint.c .Module "mod-stateful-util" unregistered14:49:44.997 sip_endpoint.c .Module "mod-refer" unregistered14:49:44.997 sip_endpoint.c .Module "mod-mwi" unregistered14:49:44.997 sip_endpoint.c .Module "mod-presence" unregistered14:49:44.997 sip_endpoint.c .Module "mod-evsub" unregistered14:49:44.997 sip_endpoint.c .Module "mod-invite" unregistered14:49:44.997 sip_endpoint.c .Module "mod-100rel" unregistered14:49:44.997 sip_endpoint.c .Module "mod-ua" unregistered14:49:44.997 sip_transaction.c .Transaction layer module destroyed14:49:44.997 sip_endpoint.c .Module "mod-tsx-layer" unregistered14:49:44.997 sip_endpoint.c .Module "mod-msg-print" unregistered14:49:44.997 sip_endpoint.c .Module "mod-pjsua-log" unregistered14:49:44.997 sip_endpoint.c .Endpoint 0x7fd5300087b8 destroyed14:49:44.997 pjsua_core.c .PJSUA state changed: CLOSING --> NULL14:49:44.997 pjsua_core.c .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()