【发布时间】:2020-04-14 14:46:06
【问题描述】:
我正在尝试为在 Stata .do 文件中执行的 python 函数实现多处理。
在 python 中,我可以执行需要一些时间的简单函数:
import multiprocessing as mp
from timeit import default_timer as timer
def square(x):
return x ** x
# Non-parallel
start = timer()
[square(x) for x in range(0,1000)]
print("Simple execution took {:.2f} seconds".format(timer()-start))
# Parallel version
pool = mp.Pool(mp.cpu_count())
start = timer()
pool.map(square, [x for x in range(0,1000)])
pool.close()
print("Multiprocessing execution took {:.2f} seconds".format(timer()-start))
一旦我尝试在 STATA .do 文件中运行相同的代码,它就会中断并返回错误:
示例.do 文件:
python:
import multiprocessing as mp
from timeit import default_timer as timer
def square(x):
return x ** x
# Non-parallel
start = timer()
[square(x) for x in range(0,1000)]
print("Simple execution took {:.2f} seconds".format(timer()-start))
# Parallel version
pool = mp.Pool(mp.cpu_count())
start = timer()
pool.map(square, [x for x in range(0,1000)])
pool.close()
print("Multiprocessing execution took {:.2f} seconds".format(timer()-start))
end
有什么想法可以找到导致错误消息的原因吗?也许还有另一种方法可以在 Stata 环境中使用 Python 进行多处理。
【问题讨论】:
-
文件看起来有问题。您确定设置了正确的权限吗?
-
我以管理员身份运行 Stata,还有什么可以改变的吗?
标签: python multiprocessing stata