有没有办法设置要与aiortc一起使用的全局变量?

时间:2019-09-27 11:39:56

标签: python-3.x aiortc

我正在尝试让python RTC客户端使用全局变量,以便可以将其重用于多个功能。

我正在将其用于我一直在进行的RTC项目,我有一个可运行的js客户端,但是功能与python不同。 服务器和js客户端上的函数是我自己的,并且没有参数,我希望避免在我制作的python客户端上使用它们。

我一直在使用来自github的aiortc Cli.py作为我的python clien应该如何工作的基础。但是我不会异步运行它,因为我试图学习和控制事件发生的时间。 源代码可以在这里找到,我指的是第71-72行中的代码 https://github.com/aiortc/aiortc/blob/master/examples/datachannel-cli/cli.py

这是我试图正常运行的代码

  

我只插入了与当前问题相关的代码


import argparse
import asyncio
import logging
import time

from aiortc import RTCIceCandidate, RTCPeerConnection, RTCSessionDescription
from aiortc.contrib.signaling import add_signaling_arguments, create_signaling

pc = None
channel = None

def createRTCPeer():
    print("starting RTC Peer")
    pc = RTCPeerConnection()
    print("created Peer", pc)
    return pc

def pythonCreateDataChannel():
    print("creating datachannel")
    channel = pc.CreateDataChannel("chat")

createRTCPeer函数按预期工作,并创建一个RTC对象,但是如果在使用它之前将其设置为“ None”,我的pythonCreateDataChannel会报告错误

  

AttributeError:“ NoneType”对象没有属性“ CreateDataChannel”

它将报告

  

NameError:未定义名称“渠道”

如果我没有事先将其设置在全局范围内,则pc也一样

1 个答案:

答案 0 :(得分:0)

您尝试过吗:

import argparse
import asyncio
import logging
import time

from aiortc import RTCIceCandidate, RTCPeerConnection, RTCSessionDescription
from aiortc.contrib.signaling import add_signaling_arguments, create_signaling

pc = None
channel = None

def createRTCPeer():
    print("starting RTC Peer")
    global pc
    pc = RTCPeerConnection()
    print("created Peer", pc)

def pythonCreateDataChannel():
    print("creating datachannel")
    global channel
    channel = pc.CreateDataChannel("chat")