Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python';s等效Java';s函数wait(),notify(),已同步_Python_Multithreading_Sync - Fatal编程技术网

Python';s等效Java';s函数wait(),notify(),已同步

Python';s等效Java';s函数wait(),notify(),已同步,python,multithreading,sync,Python,Multithreading,Sync,我必须用Python2.7编写一个类,我遇到了一些问题。 我来自java背景,最近学习了python 如果我必须使用java,我会写以下内容 public class CommandSender extends Thread { private boolean isTimeOut; private boolean running; private ArrayList<Command> waitingList; public CommandSender

我必须用Python2.7编写一个类,我遇到了一些问题。 我来自java背景,最近学习了python

如果我必须使用java,我会写以下内容

public class CommandSender extends Thread {
    private boolean isTimeOut;
    private boolean running;
    private ArrayList<Command> waitingList;

    public CommandSender() {
        running = false;
        waitingList = new LinkedList<Command>();
        isTimeOut = false;
    }

    public void run() {
        running = true;
        while (running) {
            synchronized (this) {
                 while (waitingList.isEmpty() && running) {
                     try {
                         wait();
                     } catch (InterruptedException ie) {
                         ie.printStackTrace();
                     }
                  }
                  while (!waitingList.isEmpty() && running) {
                      currentCmd = waitingList.remove(0);
                      // doSomething(currentCmd)
                  }
             }
        }
    }

    public synchronized void sendCommand(Command cmd) {
        waitingList.add(cmd);
        notify();
    }

    public synchronized boolean isTimeOut() {
        return isTimeOut;
    }
}
我每个实例使用一个锁,因为CommandSender只有一个实例

那么,如何进行等待/通知过程呢?我的同步块使用得好吗


谢谢

首先,您应该知道Python不允许多个线程同时运行Python代码(尽管线程可以运行例如C代码,例如,如果它们适当地释放GIL,则可以使用本机代码模块)。如果需要使用Python代码使用多核CPU,请查看模块


现在,直接等价物是类。创建一个
事件
对象,然后使用
等待
设置
这听起来像一个简单的生产者-消费者队列。在爪哇,你应该考虑使用。在Python中,可以为线程程序使用类,为使用
多处理的程序使用类

除非这是家庭作业,并且要求您自己使用特定的锁定机制来实现代码。我所知道的实现生产者-消费者队列的最简单方法是使用两个或三个信号量:

  • 一个信号量,用于计算队列中的元素数。最初为零
  • 一个信号量,用于计算队列中元素的限制/最大数量,并以此最大数量初始化。如果您不需要有限的队列,这是可选的
  • 一个信号量、互斥或关键部分,用于共享对内部队列/链表的访问。在Python中,线程程序不需要这样做,因为GIL允许您向列表中添加元素,而无需同步线程

旁注:我个人认为使用锁定对象(无论语言如何)比同步整个对象更好。否则,如果您的对象在外部用于同步,则会导致奇怪的结果。确实如此,但生产者和使用者位于同一线程中,与主程序不同。基本上,我的主程序在CommandSender发送命令时做一些事情。@Zycho,有什么问题吗?想详细说明一下吗?您可以在同一线程中添加和删除队列中的元素。
class CommandSender(threading.Thread)

     def __init__(self):
         threading.Thread.__init__(self)
         self.waiting_list = []
         self.running = False
         self.is-time_out = False
         self.my_lock = threading.Lock()

     def run(self):
         self.running = True
         with self.my_lock:
             while len(self.waiting_list) == 0 and self.running:
                 # Don't know what I have to do here
             while len(self.waiting_list) != 0 and self.running:
                 # Do my stuff

     def send_command(self, cmd):
         with self.my_lock:
             self.waiting_list.append(cmd)
             # Notify ?

     def is_time_out(self):
         with self.my_lock:
             return self.is_rime_out