- 論壇徽章:
- 4
|
本帖最后由 linux_c_py_php 于 2012-11-18 11:21 編輯
這得用條件變量+互斥鎖才能完成.
你需要對multiprocessing存在的理由與threading進(jìn)行比較, 了解python的GIL, 并且知道m(xù)ultiprocessing的實(shí)現(xiàn)原理, 怎么實(shí)現(xiàn)進(jìn)程間共享互斥鎖和條件變量, 怎么共享普通變量, 怎么使用互斥鎖與條件變量保證事件不會丟失, 要求較高.- [root@vps616 python]# cat main.py
- #python2.7.3
- #coding=utf-8
- from multiprocessing import Process, Lock, Condition, Value
- def callback(mutex, cond, val):
- mutex.acquire()
- while not val.value:
- cond.wait()
- print "sub proc done, exit..."
- mutex.release()
- if __name__ == "__main__":
- mutex = Lock()
- cond = Condition(mutex)
- val = Value('i', 0)
- proc = Process(target = callback, args = (mutex, cond, val))
- proc.start()
- mutex.acquire()
- val.value = 1
- cond.notify()
- mutex.release()
- proc.join()
復(fù)制代碼 |
|