我对多重处理概念不熟悉。
datetime_vec <- as.POSIXct(vec, format = "%m/%d/%Y %I:%M %p")
# [1] NA NA NA
# [4] "2018-12-20 18:08:00 EST" "2018-12-21 11:49:00 EST" "2019-01-05 11:21:00 EST"
from multiprocessing import Process
def square(x):
for x in numbers:
print('%s squared is %s' % (x, x**2))
if __name__ == '__main__':
numbers = [43, 50, 5, 98, 34, 35]
p = Process(target=square, args=('x',))
p.start()
p.join
print "Done"
我了解,我们可以使用Done
43 squared is 1849
50 squared is 2500
5 squared is 25
98 squared is 9604
34 squared is 1156
35 squared is 1225
来获取系统中的CPU数量
但是,我未能实现2项有趣的事情。 -
答案 0 :(得分:3)
您的示例中有几件事不对。
p.join()
中的括号,因此从未等待该过程(这就是为什么首先打印Done
的原因)。您应该改用multiprocessing.Pool
,像这样。
from multiprocessing import Pool
def square(x):
print('%s squared is %s' % (x, x**2))
if __name__ == '__main__':
numbers = range(1, 1000, 50)
with Pool() as p:
for value in p.imap_unordered(square, numbers):
# You could do something with the
# return value from `square` here.
pass
print("Done")
此输出(例如-不能保证订单)
1 squared is 1
51 squared is 2601
101 squared is 10201
151 squared is 22801
201 squared is 40401
251 squared is 63001
401 squared is 160801
451 squared is 203401
501 squared is 251001
301 squared is 90601
551 squared is 303601
601 squared is 361201
351 squared is 123201
651 squared is 423801
701 squared is 491401
751 squared is 564001
801 squared is 641601
851 squared is 724201
901 squared is 811801
951 squared is 904401
Done
Pool()
默认使用cpu_count
进程,因此您不必为此担心。square()
现在仅处理一个数字。它应该真正返回以进行打印和处理,而不是单独打印,但这是一个简单的示例。.map()
上使用.imap()
,Pool
或其他一些方法;我之所以选择.imap_unordered()
,是因为我不在乎获取这些值的顺序(而且,无论如何,我什么也不做)。 没有什么特别的将单个进程“锁定”到单个CPU,尽管–毕竟,单个进程可以利用多个线程,OS调度程序可以将这些线程调度到不同的CPU上。不过,不同的OS都有API来限制每个进程(和线程)的处理器。如果确实需要,可以深入研究。