有三个Python程序,写入器程序(writer.py)写入文件output.txt,两个读取器程序同时(reader_1.py, reader_2.py)读取文件。output.txt file
实现这三个程序之间同步的最佳方法是什么? 如果其他程序正在写入输出文件,如何避免读取器程序读取? 如何在Python中有效处理单作者和多读者问题?
我尝试实现fnctl锁定机制,但是在我的python中找不到这个模块。
作家.py
#!/usr/bin/python
import subprocess
import time
cycle = 10
cmd="ls -lrt"
def poll():
with open("/home/output.txt", 'a') as fobj:
fobj.seek(0)
fobj.truncate()
try:
subprocess.Popen(cmd, shell=True, stdout=fobj)
except Exception:
print "Exception Occured"
# Poll the Data
def do_poll():
count = int(time.time())
while True:
looptime = int(time.time())
if (looptime - count) >= cycle:
count = int(time.time())
print('Begin polling cycle')
poll()
print('End polling cycle')
def main():
do_poll()
if __name__ == "__main__": …Run Code Online (Sandbox Code Playgroud) Curly大括号{}不能在C语言正则表达式中工作,如果我给出正确的输入为"ab"或"ac",它总是给出输出为NO匹配.在这种情况下我会请求帮助.
#include <sys/types.h>
#include <regex.h>
#include <stdio.h>
int main(int argc, char *argv[]){ regex_t regex;
int reti;
char msgbuf[100];
/* Compile regular expression */
reti = regcomp(®ex, "[a-c]{2}", 0);
if( reti ){ fprintf(stderr, "Could not compile regex\n"); return(1); }
/* Execute regular expression */
reti = regexec(®ex, "ab", 0, NULL, 0);
if( !reti ){
puts("Match");
}
else if( reti == REG_NOMATCH ){
puts("No match");
}
else{
regerror(reti, ®ex, msgbuf, sizeof(msgbuf));
fprintf(stderr, "Regex match failed: %s\n", msgbuf);
return 1;
}
/* Free …Run Code Online (Sandbox Code Playgroud)