从python调用BCP会引发超时异常,然后立即完成

时间:2014-04-25 23:19:23

标签: python subprocess command-prompt bcp python-3.4

我正在运行一个调用子进程的python脚本。它超时,然后我立即看到子流程(在这种情况下是BCP调用)完成。实际上,我可以在数据库中看到它完整。另外,我可以直接在命令行上运行BCP命令,它运行正常。

这是我的python脚本向命令提示符吐出的内容:

C:\FaceIAPS\StudyDataFiles> 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-2\BEHAVIORRESEARCH -e bulk_copy_errors.log", shell=True, timeout=5)
  File "C:\Python34\lib\subprocess.py", line 554, in check_call
    retcode = call(*popenargs, **kwargs)
  File "C:\Python34\lib\subprocess.py", line 537, in call
    return p.wait(timeout=timeout)
  File "C:\Python34\lib\subprocess.py", line 1157, in wait
    raise TimeoutExpired(self.args, timeout)
subprocess.TimeoutExpired: Command 'bcp BehaviorResearch..Facet_Data in _temp_ -T -c -S     PBB-C202B-2\BEHAVIORRESEARCH -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:\FaceIAPS\StudyDataFiles> 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-2\BEHAVIORRESEARCH", "-e bulk_copy_errors.log"]
subprocess.call(arguments, timeout=30)

作为未经证实的FYI,&#34; in&#34;这是它自己的论点。

1 个答案:

答案 0 :(得分:4)

subprocess.check_call() say的文档:

  

timeout参数传递给Popen.wait()。如果超时   到期后,子进程将被杀死,然后再等待。   子进程后将重新引发TimeoutExpired异常   已经终止。

subprocess source code证实了这一点:

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进程本身。

在子进程已退出(我不确定)后,您会看到缓冲输出在控制台中刷新,您看到来自alive grandchild bcp进程的输出,而其父进程为{{1}已完成(显示新提示)。

删除cmd.exe以避免创建不必要的中间shell=True进程,以便直接在bcp进程上调用cmd.exe而不是shell进程。