找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 3388|回复: 4

[求助] Python Semaphore信号量的一个示例代码运行卡死的问题

4

主题

9

帖子

9

积分

贫民

积分
9
DavidLee 发表于 2017-2-21 21:21:02 | 显示全部楼层 |阅读模式
from multiprocessing import Process, Semaphore, Lock, Queue
import time

buffer = Queue(10)
empty = Semaphore(2)
full = Semaphore(0)
lock = Lock()

class Consumer(Process):

    def run(self):
        global buffer, empty, full, lock
        while True:
            full.acquire()
            lock.acquire()
            buffer.get()
            print('Consumer pop an element')
            time.sleep(1)
            lock.release()
            empty.release()


class Producer(Process):
    def run(self):
        global buffer, empty, full, lock
        while True:
            empty.acquire()
            lock.acquire()
            buffer.put(1)
            print('Producer append an element')
            time.sleep(1)
            lock.release()
            full.release()


if __name__ == '__main__':
    p = Producer()
    c = Consumer()
    p.daemon = c.daemon = True
    p.start()
    c.start()
    p.join()
    c.join()
    print 'Ended!'

每次运行之后代码打印

Producer append an element
Producer append an element

之后,就卡在这个地方不动了,请问是什么原因?


回复

使用道具 举报

1419

主题

1891

帖子

291

积分

侠客

积分
291

最佳新人热心会员默默耕耘

whydo1 发表于 2017-2-22 10:35:57 | 显示全部楼层
用全局变量不行,用参数可以

  1. from multiprocessing import Process, Semaphore, Lock, Queue
  2. import time, random

  3. class Consumer(Process):
  4.     def __init__(self, buffer, empty, full, lock):
  5.         Process.__init__(self)
  6.         self.buffer = buffer
  7.         self.empty = empty
  8.         self.full = full
  9.         self.lock = lock

  10.     def run(self):
  11.         print('now in c')
  12.         while True:
  13.             print('now in c while')
  14.             self.full.acquire()
  15.             #self.lock.acquire()
  16.             if self.buffer.qsize != 0:
  17.                 print('从队列取出一个元素 %f。当前队列长度:%i' %(self.buffer.get(), self.buffer.qsize()))
  18.             time.sleep(5)
  19.             #self.lock.release()
  20.             self.empty.release()


  21. class Producer(Process):
  22.     def __init__(self, buffer, empty, full, lock):
  23.         Process.__init__(self)
  24.         self.buffer = buffer
  25.         self.empty = empty
  26.         self.full = full
  27.         self.lock = lock

  28.     def run(self):
  29.         print('now in p')
  30.         while True:
  31.             print('now in p while')
  32.             self.empty.acquire()
  33.             #self.lock.acquire()
  34.             rd = random.random()
  35.             self.buffer.put(rd)
  36.             print('给队列追加一个元素 %f,当前队列长度:%i' %(rd, self.buffer.qsize()))
  37.             time.sleep(1)
  38.             #self.lock.release()
  39.             self.full.release()


  40. if __name__ == '__main__':
  41.     buffer = Queue(10)
  42.     empty = Semaphore(5)
  43.     full = Semaphore(0)
  44.     lock = Lock()
  45.     p = Producer(buffer, empty, full, lock)
  46.     c = Consumer(buffer, empty, full, lock)
  47.     p.daemon = c.daemon = True
  48.     p.start()
  49.     c.start()
  50.     p.join()
  51.     c.join()
  52.     print('Ended!')
复制代码
python3.4.4, win10
回复 支持 1 反对 0

使用道具 举报

4

主题

9

帖子

9

积分

贫民

积分
9
DavidLee  楼主| 发表于 2017-2-22 10:58:35 | 显示全部楼层
好的,我试下用参数
回复 支持 反对

使用道具 举报

4

主题

9

帖子

9

积分

贫民

积分
9
DavidLee  楼主| 发表于 2017-2-22 11:09:32 | 显示全部楼层
whydo1 发表于 2017-2-22 10:35
用全局变量不行,用参数可以

非常感谢版主,按照你说的不用全局变量,用参数,代码可以正常运行了。但是为什么不可以用全局变量呢?

Producer append an element
Producer append an element
Consumer pop an element
Consumer pop an element
回复 支持 反对

使用道具 举报

1419

主题

1891

帖子

291

积分

侠客

积分
291

最佳新人热心会员默默耕耘

whydo1 发表于 2017-2-22 11:58:33 | 显示全部楼层
不太清楚具体原因
python3.4.4, win10
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表