Last active
August 29, 2015 14:11
-
-
Save python012/4cf7fe6a4dc63ef047b4 to your computer and use it in GitHub Desktop.
Try the code with multiprocessing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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.' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What's wrong with my code? Why print bad_path_list as [] finally?