Python 全局变量线程安全

时间:2021-03-25 01:50:58

标签: python vlc thingsboard

我的脚本使用了 thingsboard python sdk 和 vlc,但仍然想知道我的全局变量是否需要同步以实现线程安全

import time
import logging
import vlc
from tb_device_mqtt import TBDeviceMqttClient

def on_server_side_rpc_request(request_id, request_body):
     global ins, player, media
     media = ins.media_new(request_body["params"]["url"])
     player.set_media(media)
     player.play()            
client = TBDeviceMqttClient("127.0.0.1", "A1_TEST_TOKEN")
client.set_server_side_rpc_request_handler(on_server_side_rpc_request)
client.connect()
ins = vlc.Instance("--aout=alsa --verbose=9")
player = ins.media_player_new()
media = ins.media_new(DEFAULT_STREAM_URI)
player.set_media(media)
while True:
    if player.get_state() == State.Ended:
       player.play()
    time.sleep(10)

thingsboard python sdk:https://github.com/thingsboard/thingsboard-python-client-sdk

不知道是否需要锁定/解锁变量 ins、media、player ?

非常感谢您的帮助

0 个答案:

没有答案