在多线程中使用“Fabric put”复制由创建的临时文件时,找不到文件tempfile.mkstemp文件到远程fi

2024-05-07 14:59:03 发布

您现在位置:Python中文网/ 问答频道 /正文

Python版本:2.6.4

织物版本:1.9.0你知道吗

我有一个自动化测试框架,通过使用穿线。穿线(在我的例子中是3个线程)。你知道吗

每个线程工作者都使用fabric put(尽管我们在这个函数上做了一些包装)来复制由创建的临时文件tempfile.mkstemp文件到远程文件。你知道吗

问题是,它总是给我一个错误,文件找不到,错误发生在'放'从异常提示。你知道吗

以下是“put”的代码:

MyShell.py(Parent class of MyFabShell)

def putFileContents(self, file, contents):

    fd, tmpFile= tempfile.mkstemp()

    os.system('chmod 777 %s' % tmpFile)

    contentsFile = open(tmpFile, 'w')
    contentsFile.write(contents)
    contentsFile.close()

    dstTmpFile = self.genTempFile()

    localshell = getLocalShell()

    self.get(localshell, tmpFile, dstTmpFile) # use local shell to put tmpFile to dstTmpFile


    os.close(fd)

    os.remove(tmpFile)
    #os.remove(tmpFile)

MyFabShell.py:

def get( self, srcShell, srcPath, dstPath ):
    srcShell.put( self, srcPath, dstPath )

def put(self, dstShell, srcpath, dstpath):

    if not self.pathExists( srcPath ):  # line 158
        raise Exception( "Cannot put <%s>, does not exist." % srcPath )

    # do fabric get/put in the following
    # ...

调用put导致错误:

...
self.shell.putFileContents(configFile, contents)
File "/path/to/MyShell.py", line 401, in putFileContents
self.get(localShell, tmpFile, dstTmpFile)
File "/path/to/MyFabShell.py", line 134, in get
srcShell.put( self, srcPath, dstPath )
File "/path/to/myFabShell.py", line 158, in put
raise Exception( "Cannot put <%s>, does not exist." % srcPath )
Exception: Cannot put </tmp/tmpwt3hoO>, does not exist.

我最初怀疑文件是否可以在put期间删除,所以我对os.remove进行了注释。然而,我又犯了同样的错误。 从异常日志来看,这不应该是“fabric put”的问题,因为异常在fabric get/put执行之前抛出 当涉及多线程时mkstemp是否不安全?但是文件说“在文件的创建过程中没有竞争条件”或者我的案例失败是因为GIL吗?我怀疑这是因为当我只使用一个线程,一切都会很好。你知道吗

有人能给我一些关于我的错误的线索吗?我为这个问题挣扎了一段时间


Tags: 文件toinpyselfgetputos
1条回答
网友
1楼 · 发布于 2024-05-07 14:59:03

当我显式地“加入”线程时,问题就解决了。我的所有线程都不是守护进程线程,每个线程都有相当多的I/O操作(例如文件写入/读取)“join”将确保每个线程的作业都已完成。 我仍然不确定我的问题的根本原因…临时文件实际上在那里,但线程抱怨“找不到”当多个线程一起工作时,我能给出的唯一猜测是: 当线程a执行I/O操作时,线程a将释放GIL,以便线程B(或C、D…)可以在a的I/O操作期间获取GIL。问题可能发生在I/O期间,因为线程A不再位于Python解释器中…这就是A“找不到文件”的原因,但是,当我们显式地加入A时,A将始终确保通过重新输入GI(全局解释器)来完成其工作。你知道吗

相关问题 更多 >