Skip to content

Instantly share code, notes, and snippets.

@CooperLuan
Created June 26, 2015 03:53
Show Gist options
  • Select an option

  • Save CooperLuan/2155260acf86785e8429 to your computer and use it in GitHub Desktop.

Select an option

Save CooperLuan/2155260acf86785e8429 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
from multiprocessing import Pool
msg_list = [
'add|(1,2,3)',
'sqrt|(3,)',
'sqrt|(5,)',
'add|(4,5,6)',
]
# 需要分布运行的任务
def job_add(*args):
return sum(args)
def job_sqrt(n):
return n ** 2
# 从消息队 pop 任务
def dequeue():
"""
fake redis.pop('job')
"""
global msg_list
try:
yield msg_list.pop()
except:
pass
# worker 用来根据消息 找到执行任务的函数、构造参数、执行
def worker(*args):
for msg in dequeue():
print('job %s' % msg)
func, arg_str = msg.split('|')
func = globals().get('job_' + func)
args = eval(arg_str)
result = func(*args)
print('job %s result %s' % (msg, result))
def main():
with Pool(processes=2) as pool:
pool.map(worker, [None] * 4)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment