如何创建一个子进程可以读取的临时文件? [英] How to create a temporary file that can be read by a subprocess?

查看:22
本文介绍了如何创建一个子进程可以读取的临时文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 Python 脚本,需要将一些数据写入临时文件,然后创建一个运行 C++ 程序的子进程来读取临时文件.我正在尝试为此使用 NamedTemporaryFile,但是根据文档,

I'm writing a Python script that needs to write some data to a temporary file, then create a subprocess running a C++ program that will read the temporary file. I'm trying to use NamedTemporaryFile for this, but according to the docs,

是否可以使用名称再次打开文件,而命名的临时文件仍然打开,因平台而异(它可以在 Unix 上使用;它不能在 Windows NT 或更高版本上使用).

Whether the name can be used to open the file a second time, while the named temporary file is still open, varies across platforms (it can be so used on Unix; it cannot on Windows NT or later).

事实上,在 Windows 上,如果我在写入后刷新临时文件,但在我希望它消失之前不要关闭它,子进程将无法打开它进行读取.

And indeed, on Windows if I flush the temporary file after writing, but don't close it until I want it to go away, the subprocess isn't able to open it for reading.

我正在通过使用 delete=False 创建文件,在生成子进程之前关闭它,然后在完成后手动删除它来解决这个问题:

I'm working around this by creating the file with delete=False, closing it before spawning the subprocess, and then manually deleting it once I'm done:

fileTemp = tempfile.NamedTemporaryFile(delete = False)
try:
    fileTemp.write(someStuff)
    fileTemp.close()
    # ...run the subprocess and wait for it to complete...
finally:
    os.remove(fileTemp.name)

这看起来不雅.有一个更好的方法吗?也许是一种打开临时文件权限的方法,以便子进程可以获取它?

This seems inelegant. Is there a better way to do this? Perhaps a way to open up the permissions on the temporary file so the subprocess can get at it?

推荐答案

至少如果您使用现有 Python 库打开临时文件,则在 Windows 的情况下无法从多个进程访问它.根据 MSDN,您可以指定第三个参数(dwSharedModecode>) 共享模式标志 FILE_SHARE_READCreateFile() 函数:

At least if you open a temporary file using existing Python libraries, accessing it from multiple processes is not possible in case of Windows. According to MSDN you can specify a 3rd parameter (dwSharedMode) shared mode flag FILE_SHARE_READ to CreateFile() function which:

启用对文件或设备的后续打开操作以请求读取使用权.否则,其他进程无法打开文件或设备,如果他们请求读访问.如果未指定此标志,但文件或设备已打开读取访问,该功能失败.

Enables subsequent open operations on a file or device to request read access. Otherwise, other processes cannot open the file or device if they request read access. If this flag is not specified, but the file or device has been opened for read access, the function fails.

因此,您可以编写特定于 Windows 的 C 例程来创建自定义临时文件打开器函数,从 Python 调用它,然后您可以让子进程访问该文件而不会出现任何错误.但我认为你应该坚持你现有的方法,因为它是最便携的版本,可以在任何系统上运行,因此是最优雅的实现.

So, you can write a Windows specific C routine to create a custom temporary file opener function, call it from Python and then you can make your sub-process access the file without any error. But I think you should stick with your existing approach as it is the most portable version and will work on any system and thus is the most elegant implementation.

  • 关于 Linux 和 windows 文件锁定的讨论可以在 此处.

原来可以打开 &也从 Windows 中的多个进程读取临时文件.请参阅 Piotr Dobrogost 的答案.

Turns out it is possible to open & read the temporary file from multiple processes in Windows too. See Piotr Dobrogost's answer.

这篇关于如何创建一个子进程可以读取的临时文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆