Skip to content

Instantly share code, notes, and snippets.

@python012
Last active August 29, 2015 14:11
Show Gist options
  • Save python012/4cf7fe6a4dc63ef047b4 to your computer and use it in GitHub Desktop.
Save python012/4cf7fe6a4dc63ef047b4 to your computer and use it in GitHub Desktop.
Try the code with multiprocessing
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from multiprocessing import Process
from multiprocessing import Queue
from multiprocessing import Pool
import multiprocessing
path_list = range(10)
bad_path_list = []
def add_to_bad_path_list(bad_path, lock):
lock.acquire()
print "%s is added." % str(bad_path)
global bad_path_list
bad_path_list.append(bad_path)
print bad_path_list
lock.release()
def check_path(path_list_q, lock): #对目标队列进行检查,条件简化为3的倍数
while True:
if not path_list_q.empty():
value = path_list_q.get()
if 0 == value % 3:
add_to_bad_path_list(value, lock)
else:
break
if __name__ == '__main__':
manager = multiprocessing.Manager()
path_list_q = manager.Queue()
for i in path_list: #初始化待检查队列
path_list_q.put(i)
lock = manager.Lock()
p = Pool()
p1 = p.apply_async(check_path, args=(path_list_q, lock))
p.close()
p.join()
print bad_path_list
print 'Done.'
@python012
Copy link
Author

What's wrong with my code? Why print bad_path_list as [] finally?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment