简体   繁体   English

使用线程模块在python中实现线程

[英]Implementing threads in python with threading module

In a python program I need 2 threads based on Threading module. 在python程序中,我需要2个基于Threading模块的线程。 Thread#1 generates some data and put it in a buffer and thread#2 is supposed to process the data in the buffer. 线程#1生成一些数据并将其放入缓冲区,线程#2应该处理缓冲区中的数据。

So my pseudocode is like this: Thread 1: 所以我的伪代码是这样的:线程1:

Thread#1
while True:
   Generate_Some_Data()
   while flag==1:
      pass()
   Buffer_Address=Write_It_To_Buffer()
   flag=1


Thread#2
while True:
   while flag==0:
      pass()
   Process_Data(Buffer_Address)
   flag=0

(Let us assume that access to the variable "flag" is atomized with suitable locks.) (让我们假设使用合适的锁来访问变量“flag”。)

I know that Threading module is not concurrent. 我知道Threading模块不是并发的。 Essentially, this means that unless one of the threads does not block on some external condition (such as file-io or time.sleep), both threads will share the total process time regardless of the tasks. 实质上,这意味着除非其中一个线程在某些外部条件(例如file-io或time.sleep)上没有阻塞,否则无论任何任务如何,两个线程都将共享总进程时间。 Therefore, according to my understanding, approximately half of the total processing time will be wasted on "while flag" loops in the above configuration. 因此,根据我的理解,在上述配置中,大约一半的总处理时间将浪费在“while flag”循环上。

So, here are my questions: 所以,这是我的问题:

  1. Am I right in my above anticipation/understanding of the Threading module? 我对上述线程模块的预期/理解是对的吗? Is the half of the total process time wasted in "while flag" loops? 在“while flag”循环中浪费了总进程时间的一半吗?

  2. (If I am right) Is there anyway to get rid of "while flag" loops completely? (如果我是对的)无论如何都要完全摆脱“while flag”循环? I tried to find out another structure in which I can make threads sleep in lock.acquire() methods, however, I could not figure out a %100 safe way of doing so. 我试图找出另一种结构,我可以在lock.acquire()方法中让线程睡眠,但是,我无法弄清楚%100安全的方法。 (When one thread relases lock, there is no quarantee that the other thread will acquire it before the same thread acquire it again) (当一个线程重新锁定时,没有保证另一个线程将在同一线程再次获取之前获取它)

It sounds like the Queue module is what you need. 听起来像Queue模块就是您所需要的。

This will give you a blocking FIFO queue. 这将为您提供阻塞FIFO队列。 Whenever the consumer thread gets something from the queue, if there's nothing in there, it will block until something becomes available, giving the producer thread time to produce something. 每当消费者线程从队列中获取某些内容时,如果其中没有任何内容,它将阻塞直到某些内容可用,从而为生产者线程提供生成内容的时间。

If you're concerned about the producer thread hogging all the compute cycles, you can put a limit on the size of the queue. 如果您担心生产者线程占用所有计算周期,则可以限制队列的大小。 If the queue is full, and the producer thread has something to add, it will block until the consumer thread takes something out. 如果队列已满,并且生产者线程有要添加的内容,它将阻塞,直到使用者线程取出某些内容。

Here's some pseudocode: 这是一些伪代码:

import Queue
q = Queue.Queue()

# Producer thread
while True:
    data = generate_some_data()
    q.put(data)

# Consumer thread
while True:
    data = q.get()
    process_data(data)

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

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