[英]Calling python script from TCL and creating a file atomically
I wrote a python script that takes care of creating a file atomically as described in this other question . 我编写了一个python脚本,该脚本负责按照另一个问题中的描述自动创建文件。 The file that gets created by the script is somewhat big (about ~1.5MB) as seen below:
脚本创建的文件有些大(约1.5MB),如下所示:
$ ./my_script.py --output_file some_directory/my_file.txt
$ ls -l --block-size=K some_directory/my_file.txt
-rw------- 1 foouser foogroup 1477K Aug 7 17:39 some_directory/my_file.txt
Unfortunately, due to some legacy infrastructure and the way things are set up, this script needs to be called from within a TCL script. 不幸的是,由于某些传统基础架构和设置方式,需要从TCL脚本中调用此脚本。 Here is the way in which I am currently doing that:
这是我目前正在执行的操作:
#!/usr/bin/env tclsh
set PY_SCRIPT my_script.py
puts [exec $PY_SCRIPT --output some_directory/my_file.txt]
The problem that I am seeing is that the generated file does not seem to be created atomically anymore (atomicity is a requirement for my application). 我看到的问题是生成的文件似乎不再是原子创建的(原子性是我的应用程序的要求)。 The TCL puts command documentation says the following:
TCL命令命令文档中的内容如下:
Tcl buffers output internally, so characters written with puts may not appear immediately on the output file or device;
Tcl缓冲区在内部输出,因此用put编写的字符可能不会立即出现在输出文件或设备上; Tcl will normally delay output until the buffer is full or the channel is closed.
Tcl通常会延迟输出,直到缓冲区已满或通道关闭为止。
I believe this explains why my file is not being atomically created anymore. 我相信这可以解释为什么我的文件不再被自动创建。 I'm not an expert in TCL, so I am not sure how to accomplish atomic file creation in the TCL script.
我不是TCL的专家,所以我不确定如何在TCL脚本中完成原子文件的创建。
Any suggestions? 有什么建议么?
我认为您想要的只是flush stdout
。
A direct translation of the Python code that you link to would be something like this (untested code): 您链接到的Python代码的直接翻译是这样的(未经测试的代码):
package require Tclx
set f [open $tmpFile w]
puts -nonewline $f $text
sync $f ; # flush is performed implicitly
close $f
file rename -force $tmpFile $myFile
The TclX package is needed to get the sync
command. 需要TclX软件包才能获取
sync
命令。 (This will probably not work on Windows.) (这可能在Windows上不起作用。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.