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

Python 3.x 有没有办法设置要与aiortc一起使用的全局变量?,python-3.x,aiortc,Python 3.x,Aiortc,我试图让python RTC客户机使用全局变量,以便可以将其用于多个函数 我将其用于我一直在从事的一个RTC项目,我有一个运行正常的js客户端,但函数的工作方式与python不同。 服务器端和js客户端上的函数是我自己的,没有参数,我希望避免在我正在制作的python客户端上使用它们 我一直在使用github中的aiortc Cli.py作为python客户端工作方式的基础。但我不会异步运行它,因为我试图学习和控制事件发生的时间。 源代码可以在这里找到,我指的是第71-72行中的代码 这是我正

我试图让python RTC客户机使用全局变量,以便可以将其用于多个函数

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

我一直在使用github中的aiortc Cli.py作为python客户端工作方式的基础。但我不会异步运行它,因为我试图学习和控制事件发生的时间。 源代码可以在这里找到,我指的是第71-72行中的代码

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

我只插入了与当前版本相关的代码

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

AttributeError:“非类型”对象没有属性“CreateDataChannel”

它会报告的

NameError:未定义名称“通道”

如果我事先没有将pc设置为全局范围,则pc也是如此。您是否尝试过以下方法:

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")
你试过这个吗:

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")