我们说我测试了以下代码以查看子流程池的行为:
# coding=utf-8
import os
import sys
from multiprocessing import Pool
import time
import random
def run_proc(param1):
print("child procees %s pid is %s,parent id is %s" %
(param1, os.getpid(), os.getppid()))
starttime = time.time()
time.sleep(random.random() * 3)
endtime = time.time()
print('child process %s runs %0.2f seconds.' %
(param1, (endtime - starttime)))
if __name__ == '__main__':
print(sys.version)
pname = sys.argv[0].split('/')[-1]
print("process %s is running now...,it's pid is %s" % (pname, os.getpid()))
p = Pool(5)
for i in range(5):
p.apply_async(run_proc, args=("test"+str(i),))
print("waiting for all subprocess to end...")
p.close()
p.join()
print("all subprocesses are over!")
而且输出完全符合我的预期:
3.5.0 (default, Jul 23 2017, 10:55:33)
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)]
process mp_basic_pool.py is running now...,it's pid is 19352
waiting for all subprocess to end...
child procees test0 pid is 19367,parent id is 19352
child procees test1 pid is 19368,parent id is 19352
child procees test2 pid is 19369,parent id is 19352
child procees test3 pid is 19370,parent id is 19352
child procees test4 pid is 19371,parent id is 19352
child process test2 runs 0.93 seconds.
child process test4 runs 1.33 seconds.
child process test3 runs 1.68 seconds.
child process test0 runs 2.68 seconds.
child process test1 runs 2.90 seconds.
all subprocesses are over!
[Finished in 3.2s]
有一行"p.apply_async(run_proc, args=("test"+str(i),))"
。当我第一次编写这段代码时,我把它写成:"p.apply_async(run_proc, args=("test"+str(i)))"
。留下逗号,但输出为:
3.5.0 (default, Jul 23 2017, 10:55:33)
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)]
process mp_basic_pool.py is running now...,it's pid is 19382
waiting for all subprocess to end...
all subprocesses are over!
[Finished in 0.4s]
我找了python文件,发现第二个参数应该是一个元组,但是需要逗号吗?
答案 0 :(得分:1)
单个元素元组(Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
ActiveCell.Select
If Range("O22") <> "" Then
ActiveSheet.Unprotect
Range("F22,G22,J22,K22,L22,O22").Select
Selection.Locked = True
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
End If
End Sub
是)需要一个尾随逗号来区分它们与一对括号。
以这种方式思考:对于("test"+str(i),)
,如果没有逗号,如果您打算使用括号进行分组或制作元组,那么解释器应该如何知道呢?它含糊不清。