小编use*_*713的帖子

如何在Python中一次读取和写入同一个文件

有三个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)

python python-2.7 python-3.x

5
推荐指数
0
解决办法
268
查看次数

Curley大括号在C语言中不能用于正则表达式

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(&regex, "[a-c]{2}", 0);
        if( reti ){ fprintf(stderr, "Could not compile regex\n"); return(1); }

        /* Execute regular expression */
        reti = regexec(&regex, "ab", 0, NULL, 0);
        if( !reti ){
                puts("Match");
        }
        else if( reti == REG_NOMATCH ){
                puts("No match");
        }
        else{
                regerror(reti, &regex, msgbuf, sizeof(msgbuf));
                fprintf(stderr, "Regex match failed: %s\n", msgbuf);
                return 1;
        }

       /* Free …
Run Code Online (Sandbox Code Playgroud)

c regex

4
推荐指数
1
解决办法
212
查看次数

标签 统计

c ×1

python ×1

python-2.7 ×1

python-3.x ×1

regex ×1