我希望你能帮助我。
我有一个msgList
包含msg
对象的对象,每个对象都有pos
和content
属性。然后,我有一个函数posClassify
,该函数创建一个SentimentClassifier
对象,msgList
并以此为对象进行迭代,并msgList[i].pos = clf.predict(msgList[i].content)
作为clf
的实例SentimentClassifier
。
def posClassify(msgList):
clf = SentimentClassifier()
for i in tqdm(range(len(msgList))):
if msgList[i].content.find("omitted") == -1:
msgList[i].pos = clf.predict(msgList[i].content)
我想要的是使用多重处理来计算。我读过您创建一个池,并使用要传递给该函数的参数列表调用函数,仅此而已。我认为该功能必须类似于保存图像或在不同的内存空间上工作,而不像我的那样,您需要在其中修改同一msg
对象,并且还必须使用该SentimentClassifier
对象(大约需要10秒钟左右的时间)初始化)。
我的想法是在创建cpu_cores-1进程的过程中,每个进程都使用的实例SentimentClassifier
,然后每个进程开始使用带有自己的分类器的msg列表进行消费,但是我无法解决该问题。我还考虑过创建带有二进制信号量的线程,每个线程都调用其自己的分类器,然后等待该信号量更新对象中的pos
值msg
,但仍无法弄清楚。
您可以在python中使用ProcessPoolExecutor
fromfutures
模块。
ProcessPoolExecutor
是Executor子类使用最多max_workers进程的池异步执行调用。如果max_workers为None或未提供,它将默认为计算机上的处理器数量
您可以在Python文档中找到更多信息
这里是实现并发性的示例代码,假设每个msgList[i]
都与msgList[j]
when无关i != j
,
from concurrent import futures
def posClassify(msg, idx, clf):
return idx, clf.predict(msg.content)
def classify(msgList):
clf = SentimentClassifier()
calls = []
executor = futures.ProcessPoolExecutor(max_workers=4)
for i in tqdm(range(len(msgList))):
if msgList[i].content.find("omitted") == -1:
call = executor.submit(posClassify, msgList[i], i, clf)
calls.append(call)
# wait for all processes to finish
executor.shutdown()
# assign the result of individual calls to msgList[i].pos
for call in calls:
result = call.result()
msgList[result[0]].pos = result[1]
为了执行代码,只需调用classify(msgList)
函数即可。
希望这对您有所帮助!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句