简体   繁体   English

如何正确使用 python 中的队列?

[英]How to correctly use queues in python?

I am a beginner when it comes to python threading and multiprocessing so please bear with me.我是 python 线程和多处理的初学者,所以请多多包涵。

I want to make a system that consists of three python scripts.我想制作一个由三个 python 脚本组成的系统。 The first one creates some data and sends this data to the second script continuously.第一个创建一些数据并将这些数据连续发送到第二个脚本。 The second script takes the data and saves on some file until the file exceeds defined memory limit.第二个脚本获取数据并保存在某个文件上,直到文件超过定义的 memory 限制。 When that happens, the third script sends the data to an external device and gets rid of this "cache".发生这种情况时,第三个脚本将数据发送到外部设备并删除此“缓存”。 I need all of this to happen concurrently.我需要所有这一切同时发生。 The pseudo code sums up what I am trying to do.伪代码总结了我正在尝试做的事情。

def main_1():

  data = [1,2,3]
  send_to_second_script(data)

def main_2():

  rec_data = receive_from_first_script()
  save_to_file(rec_data)
  if file>limit:
     signal_third_script()

def main_3():
  if signal is true:
     send_data_to_external_device()
     remove_data_from_disk()

I understand that I can use queues to make this happen but I am not sure how.我知道我可以使用队列来实现这一点,但我不确定如何。

Also, so far to do this, I tried a different approach where I created one python script and used threading to spawn threads for each part of the process.此外,到目前为止,为了做到这一点,我尝试了一种不同的方法,我创建了一个 python 脚本并使用线程为进程的每个部分生成线程。 Is this correct or using queues is better?这是正确的还是使用队列更好?

Firstly, for Python you need to be really aware what the benefits of multithreading/multiprocessing gives you.首先,对于 Python,您需要真正了解多线程/多处理给您带来的好处。 IMO you should be considering multiprocessing instead of multithreading. IMO 你应该考虑多处理而不是多线程。 Threading in Python is not actually concurrent due to GIL and there are many explanations out on which one to use.由于 GIL,Python 中的线程实际上并不是并发的,对于使用哪一个有很多解释。 Easiest way to choose is to see if your program is IO-bound or CPU-bound.最简单的选择方法是查看您的程序是受 IO 限制还是受 CPU 限制。 Anyways on to the Queue which is a simple way to work with multiple processes in python.无论如何,这是在 python 中处理多个进程的简单方法。

Using your pseudocode as an example, here is how you would use a Queue.以您的伪代码为例,这里是您将如何使用队列。

import multiprocessing



def main_1(output_queue):
    test = 0
    while test <=10: # simple limit to not run forever
        data = [1,2,3]
        print("Process 1: Sending data")
        output_queue.put(data) #Puts data in queue FIFO
        test+=1
    output_queue.put("EXIT") # triggers the exit clause

def main_2(input_queue,output_queue):
    file = 0 # Dummy psuedo variables
    limit = 1
    while True:
        rec_data = input_queue.get() # Get the latest data from queue. Blocking if empty
        if rec_data == "EXIT": # Exit clause is a way to cleanly shut down your processes
            output_queue.put("EXIT")
            print("Process 2: exiting")
            break
        print("Process 2: saving to file:", rec_data, "count = ", file)
        file += 1
        #save_to_file(rec_data)
        if file>limit:
            file = 0 
            output_queue.put(True)

def main_3(input_queue):
    while(True):
        signal = input_queue.get()

        if signal is True:
            print("Process 3: Data sent and removed")
            #send_data_to_external_device()
            #remove_data_from_disk()
        elif signal == "EXIT":
            print("Process 3: Exiting")
            break

if __name__== '__main__':

    q1 = multiprocessing.Queue() # Intializing the queues and the processes
    q2 = multiprocessing.Queue()
    p1 = multiprocessing.Process(target = main_1,args = (q1,))
    p2 = multiprocessing.Process(target = main_2,args = (q1,q2,))
    p3 = multiprocessing.Process(target = main_3,args = (q2,))

    p = [p1,p2,p3]
    for i in p: # Start all processes
        i.start()
    for i in p: # Ensure all processes are finished
        i.join()

The prints may be a little off because I did not bother to lock the std_out.因为我没有费心去锁定std_out,所以打印可能有点偏离。 But using a queue ensures that stuff moves from one process to another.但是使用队列可以确保东西从一个进程移动到另一个进程。

EDIT: DO be aware that you should also have a look at multiprocessing locks to ensure that your file is 'thread-safe' when performing the move/delete.编辑:请注意,您还应该查看多处理锁,以确保您的文件在执行移动/删除时是“线程安全的”。 The pseudo code above only demonstrates how to use queue上面的伪代码只演示了如何使用队列

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM