【发布时间】:2013-08-18 03:28:22
【问题描述】:
所以,我正在尝试编写一个使用 django 作为其 ORM 的应用程序,因为它既需要做一些幕后处理,又需要一个易于使用的前端。它的核心功能是处理数据库中的数据,在高 CPU 进程(基本上是蒙特卡罗模拟)中,我想实现多处理,特别是使用 Pool(我得到 4 个进程)。基本上我的代码是这样运行的,有大约 20 个父母的孩子:
assorted import statements to get the django environment in the script
from multiprocessing import Pool
from random import random
from time import sleep
def test(child):
x=[]
print child.id
for i in range(100):
print child.id, i
x.append(child.parent.id) #just to hit the DB
return x
if __name__ == '__main__':
parent = Parent.objects.get(id=1)
pool = Pool()
results = []
results = pool.map(test,parent.children.all())
pool.close()
pool.join()
print results
使用这样的代码,我得到间歇性的DatabaseErrors 或PicklingErrors。前者通常是“格式错误的数据库”或“丢失与 MySQL 服务器的连接”的形式,后者通常是“无法腌制模型.DoesNotExist”。它们是随机的,发生在任何进程中,当然数据库本身没有任何问题。如果我设置 pool = Pool(proccesses=1) 然后它运行,在一个线程中就好了。我还加入了各种打印语句,以确保它们中的大多数实际上都在运行。
我也一直在将test 更改为:
def test(child):
x=[]
s= random()
sleep(random())
for i in range(100):
x.append(child.parent.id)
return x
这只会使每次迭代在运行前暂停不到一秒,并且一切都很好。如果我将随机间隔降低到大约 500 毫秒,它就会开始起作用。所以,可能是并发问题,对吧?但只有 4 个进程命中。我的问题是如何在不提前大量转储数据的情况下解决这个问题?我已经用 SQLite 和 MySQL 对其进行了测试,但都遇到了这个问题。
【问题讨论】:
-
既然进程是 CPU 密集型的,那你为什么不使用
multiprocessing.Lock来避免数据库的所有竞争条件?
标签: python sql django multiprocessing pool