在多处理中从 python 中的进程创建者函数返回值
我从 http://docs.python.org/library/multiprocessing 中选择了一个片段.html#multiprocessing-examples
import multiprocessing
def queue_func(queue):
for i in range(30):
time.sleep(0.5 * random.random())
queue.put(i*i)
queue.put('STOP')
def test_queue():
q = multiprocessing.Queue()
p = multiprocessing.Process(target=queue_func, args=(q,))
p.start()
o = None
while o != 'STOP':
try:
o = q.get(timeout=0.3)
print o,
sys.stdout.flush()
except Empty:
print 'TIMEOUT'
test_queue()
是否可以在 test_queue() 函数中插入 return 语句?
这就是我想做的...... 每次客户端发送请求时,我想创建一个新进程来处理各个客户端的后端处理并将结果返回给客户端。我该怎么做?一个简单的演示受到高度赞赏。
I have picked a snippet from http://docs.python.org/library/multiprocessing.html#multiprocessing-examples
import multiprocessing
def queue_func(queue):
for i in range(30):
time.sleep(0.5 * random.random())
queue.put(i*i)
queue.put('STOP')
def test_queue():
q = multiprocessing.Queue()
p = multiprocessing.Process(target=queue_func, args=(q,))
p.start()
o = None
while o != 'STOP':
try:
o = q.get(timeout=0.3)
print o,
sys.stdout.flush()
except Empty:
print 'TIMEOUT'
test_queue()
Is it possible to insert a return statement in test_queue() function?
Here's what i wanted to do ...
Every time a client sends a request, i want to create a new process to handle the backend processing of the individual clients and return the result back to the client. How do i do that? A simple demonstration is highly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,当然可以。您只需在 while 循环后添加一个
returnwhat
即可。为了获得更有用的答案,通常最好解释一下您想要实现的目标、您为实现该目标所做的尝试以及以何种方式实现目标无效。
Yes, of course you can. You just add a
return whatever
after the while loop.To get a more useful answer, it's generally a good idea to explain what you want to achieve, what you have tried to achieve that and in what way it didn't work.