我希望你能帮助我
我有一个msgList
,包含msg
个对象,每个对象都有pos
和content
属性。
然后我有一个函数posClassify
,它创建了一个SentimentClassifier
对象,它通过这个msgList
进行迭代,并执行msgList[i].pos = clf.predict(msgList[i].content)
,作为clf
的一个实例
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列表,但我不知道如何实现这一点。我还考虑过使用二进制信号量创建线程,每个线程调用自己的分类器,然后等待信号量更新msg
对象中的pos
值,但仍然无法理解
您可以在Python中使用来自
futures
模块的ProcessPoolExecutor
{}是
你可以在Python docs找到更多信息
这里是实现并发性的示例代码,假设每个
msgList[i]
在i != j
时独立于msgList[j]
为了执行代码,只需调用
classify(msgList)
函数相关问题 更多 >
编程相关推荐