【发布时间】:2015-11-28 11:05:00
【问题描述】:
这是我第一次在 python 中使用多线程。 我在 python 2.7.9 中找到了许多与多线程和多处理相关的文档,并且还研究了multi-threading link。但我不知道如何实现我的应用程序。我有基于 self.count 的应用程序,在下面的代码中是 5。我必须创建一个 5 个线程或进程,当我调用 d.sum(1,2) 这应该 在 5 中调用 sum 函数不同的线程并并行执行并使用线程名称和结果更新结果字典中的结果。
现在我目前的方法正在连续发生。 但我想使用线程或进程将其设为并行。请帮助实现这一目标。
提前致谢。代码 sn-ps 非常感谢。
到目前为止我有代码。
class Base(object):
def __init__(self):
self.count =5 # Count = 5 is used to create a
#number of thread or process to run parallel
def sum(self, a, b):
result = {}
for i in range(0,self.count):
result[i] = a + b
return result
def diff(self, a, b):
result = {}
for i in range(0,self.count):
result[i] = a - b
return result
def mull(self, a, b):
result = {}
for i in range(0,self.count):
result[i] = a * b
return result
def division(self, a, b):
result = {}
for i in range(0,self.count):
result[i] = a / b
return result
d = Base()
print d.sum(1,2)
print d.diff(2,1)
【问题讨论】:
标签: python multithreading multiprocessing