为了账号安全,请及时绑定邮箱和手机立即绑定

从 python 调用 pj_thread_register

从 python 调用 pj_thread_register

暮色呼如 2022-12-14 21:03:09
我正在尝试在 python 中通过 pjsua2 使用 pjsip。我有线程问题。import pjsua2 as pjimport threading# pjsua2 test functiondef 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()?
查看完整描述

1 回答

?
LEATH

TA贡献1936条经验 获得超6个赞

这是 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 logging

import threading

import pjsua2


date_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 function

def 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 initialized

14: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" registered

14:49:43.262        sip_transport.c  .Transport manager created.

14:49:43.262           pjsua_core.c  .PJSUA state changed: NULL --> CREATED

DEBUG 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" registered

14:49:43.263         sip_endpoint.c  .Module "mod-tsx-layer" registered

14:49:43.263         sip_endpoint.c  .Module "mod-stateful-util" registered

14:49:43.263         sip_endpoint.c  .Module "mod-ua" registered

14:49:43.263         sip_endpoint.c  .Module "mod-100rel" registered

14:49:43.263         sip_endpoint.c  .Module "mod-pjsua" registered

14:49:43.263         sip_endpoint.c  .Module "mod-invite" registered

14:49:43.496             alsa_dev.c  ..ALSA driver found 32 devices

14:49:43.496             alsa_dev.c  ..ALSA initialized

14:49:43.496                  pjlib  ..select() I/O Queue created (0x7fd53009cf08)

14:49:43.499         sip_endpoint.c  .Module "mod-evsub" registered

14:49:43.499         sip_endpoint.c  .Module "mod-presence" registered

14:49:43.499         sip_endpoint.c  .Module "mod-mwi" registered

14:49:43.499         sip_endpoint.c  .Module "mod-refer" registered

14:49:43.499         sip_endpoint.c  .Module "mod-pjsua-pres" registered

14:49:43.499         sip_endpoint.c  .Module "mod-pjsua-im" registered

14:49:43.499         sip_endpoint.c  .Module "mod-pjsua-options" registered

14:49:43.499           pjsua_core.c  .1 SIP worker threads created

14:49:43.499           pjsua_core.c  .pjsua version 2.8 for Linux-5.3.0.53/x86_64/glibc-2.27 initialized

14:49:43.499           pjsua_core.c  .PJSUA state changed: CREATED --> INIT

DEBUG 2020-05-31,14:49:43.262 MainThread __main__.main thread started

DEBUG 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 --> STARTING

DEBUG 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" registered

14:49:43.500           pjsua_core.c  .PJSUA state changed: STARTING --> RUNNING

DEBUG 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 --> CLOSING

14: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 module

14:49:44.997      sip_transaction.c  .Stopped transaction layer module

14:49:44.997         sip_endpoint.c  .Module "mod-unsolicited-mwi" unregistered

14:49:44.997         sip_endpoint.c  .Module "mod-pjsua-options" unregistered

14:49:44.997         sip_endpoint.c  .Module "mod-pjsua-im" unregistered

14:49:44.997         sip_endpoint.c  .Module "mod-pjsua-pres" unregistered

14:49:44.997         sip_endpoint.c  .Module "mod-pjsua" unregistered

14:49:44.997         sip_endpoint.c  .Module "mod-stateful-util" unregistered

14:49:44.997         sip_endpoint.c  .Module "mod-refer" unregistered

14:49:44.997         sip_endpoint.c  .Module "mod-mwi" unregistered

14:49:44.997         sip_endpoint.c  .Module "mod-presence" unregistered

14:49:44.997         sip_endpoint.c  .Module "mod-evsub" unregistered

14:49:44.997         sip_endpoint.c  .Module "mod-invite" unregistered

14:49:44.997         sip_endpoint.c  .Module "mod-100rel" unregistered

14:49:44.997         sip_endpoint.c  .Module "mod-ua" unregistered

14:49:44.997      sip_transaction.c  .Transaction layer module destroyed

14:49:44.997         sip_endpoint.c  .Module "mod-tsx-layer" unregistered

14:49:44.997         sip_endpoint.c  .Module "mod-msg-print" unregistered

14:49:44.997         sip_endpoint.c  .Module "mod-pjsua-log" unregistered

14:49:44.997         sip_endpoint.c  .Endpoint 0x7fd5300087b8 destroyed

14:49:44.997           pjsua_core.c  .PJSUA state changed: CLOSING --> NULL

14: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()



查看完整回答
反对 回复 2022-12-14
  • 1 回答
  • 0 关注
  • 233 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信