当其中一个线程需要其他线程之一的结果时如何运行多个线程

Jax*_*. B 0 python multithreading ffmpeg python-multithreading python-3.x

有点令人困惑的问题。我想做的就是加快这一进程。

        yt_client = SoundHandler.YouTube(search=search)
        download_link_attempt = await yt_client.PlaySongByLink()
        search_url = yt_client.SearchURL()
        source, video_url = await yt_client.GetTopSearchResultAudioSource(search_url)
        yt_info, yt_info_embed = await yt_client.GetYouTubeInformation(video_url)
Run Code Online (Sandbox Code Playgroud)

但在第四行我需要第三行的变量。第四个和第五个也一样。

我已经尝试了我想到的一切,但似乎无法弄清楚。

bal*_*man 5

给定程序中的多个线程,并且人们希望在它们之间安全地通信或交换数据。

也许将数据从一个线程发送到另一个线程的最安全方法是使用队列库中的队列。为此,请创建一个由线程共享的 Queue 实例。然后,线程使用 put() 或 get() 操作在队列中添加或删除项目,如下面给出的代码所示。

示例:(取自此处

from queue import Queue
from threading import Thread
  
# A thread that produces data
def producer(out_q):
    while True:
        # Produce some data
        ...
        out_q.put(data)
          
# A thread that consumes data
def consumer(in_q):
    while True:
        # Get some data
        data = in_q.get()
        # Process the data
        ...
          
# Create the shared queue and launch both threads
q = Queue()
t1 = Thread(target = consumer, args =(q, ))
t2 = Thread(target = producer, args =(q, ))
t1.start()
t2.start()
Run Code Online (Sandbox Code Playgroud)