我正在运行一个调用子流程的python脚本。它超时了,然后我立即看到子进程(在本例中是BCP调用)完成。事实上,我可以在数据库中看到它是完整的。此外,我可以直接在命令行上运行BCP命令,它运行得很好。
以下是我的python脚本向命令提示符吐出的内容:
C:FaceIAPSStudyDataFiles> py .RUN_DATA.py
Synchronizing 80 subjects
Subject 11
Starting copy...
Traceback (most recent call last):
File ".RUN_DATA.py", line 261, in <module>
bulk_import(upload_file, 'Facet_Data')
File ".RUN_DATA.py", line 171, in bulk_import
subprocess.check_call("bcp BehaviorResearch.." + table_to_upload_to + " in " + filename_to_be_uploaded + " -T -c -S
PBB-C202B-2BEHAVIORRESEARCH -e bulk_copy_errors.log", shell=True, timeout=5)
File "C:Python34libsubprocess.py", line 554, in check_call
retcode = call(*popenargs, **kwargs)
File "C:Python34libsubprocess.py", line 537, in call
return p.wait(timeout=timeout)
File "C:Python34libsubprocess.py", line 1157, in wait
raise TimeoutExpired(self.args, timeout)
subprocess.TimeoutExpired: Command 'bcp BehaviorResearch..Facet_Data in _temp_ -T -c -S PBB-C202B-2BEHAVIORRESEARCH -e
bulk_copy_errors.log' timed out after 5 seconds
1000 rows sent to SQL Server. Total sent: 1000
1000 rows sent to SQL Server. Total sent: 2000
PS C:FaceIAPSStudyDataFiles> 1000 rows sent to SQL Server. Total sent: 3000
1000 rows sent to SQL Server. Total sent: 4000
1000 rows sent to SQL Server. Total sent: 5000
1000 rows sent to SQL Server. Total sent: 6000
1000 rows sent to SQL Server. Total sent: 7000
1000 rows sent to SQL Server. Total sent: 8000
1000 rows sent to SQL Server. Total sent: 9000
1000 rows sent to SQL Server. Total sent: 10000
1000 rows sent to SQL Server. Total sent: 11000
1000 rows sent to SQL Server. Total sent: 12000
1000 rows sent to SQL Server. Total sent: 13000
1000 rows sent to SQL Server. Total sent: 14000
1000 rows sent to SQL Server. Total sent: 15000
1000 rows sent to SQL Server. Total sent: 16000
16102 rows copied.
Network packet size (bytes): 4096
Clock Time (ms.) Total : 5164 Average : (3118.13 rows per sec.)
正如您所看到的,命令提示符已与BCP调用的输出分开
发生了什么事,我该如何解决?
编辑:我是如何修复的
将子流程调用更改为:
arguments = ["bcp", "BehaviorResearch.." + table_to_upload_to, "in", filename_to_be_uploaded, "-T", "-c", "-S PBB-C202B-2BEHAVIORRESEARCH", "-e bulk_copy_errors.log"]
subprocess.call(arguments, timeout=30)
作为外行的参考,"in"是它自己的论点。
subprocess.check_call()
的文档说:
timeout参数被传递给Popen.wait()。如果timeout到期时,子进程将被终止,然后再次等待。TimeoutExpired异常将在子进程之后重新引发已终止。
subprocess
源代码确认:
def call(*popenargs, timeout=None, **kwargs):
with Popen(*popenargs, **kwargs) as p:
try:
return p.wait(timeout=timeout)
except:
p.kill() # kill on any exception including TimeoutExpired
p.wait()
raise
也就是说,您看到的是预期的行为:如果发生超时,那么运行bcp进程的shell(%COMSPEC%
或cmd.exe
)应该立即终止,这反过来可能会终止bcp进程本身。
在子进程已经退出或(我不确定)之后,您可以在控制台中看到缓冲输出被刷新。在父进程cmd.exe
已经完成时,您可以看到活动孙进程bcp的输出(显示新提示)。
删除shell=True
以避免创建不必要的中间cmd.exe
进程,从而直接在bcp进程而不是shell进程上调用.kill()
。