Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python线程内调用子流程命令表单_Python_Multithreading_Subprocess - Fatal编程技术网

在Python线程内调用子流程命令表单

在Python线程内调用子流程命令表单,python,multithreading,subprocess,Python,Multithreading,Subprocess,我有一个非常简单的用例,其中我必须找出在过去10分钟内在2个不同目录中修改的文件 由于有两个不同的目录,因此我启动了两个单独的线程来执行此操作,并且在每个正在运行的线程中都存在检查已修改文件的逻辑 以下是相同的代码: import threading import os import time from subprocess import Popen, PIPE def getLatestModifiedFiles(seconds, _dir): files = (fle for rt

我有一个非常简单的用例,其中我必须找出在过去10分钟内在2个不同目录中修改的文件

由于有两个不同的目录,因此我启动了两个单独的线程来执行此操作,并且在每个正在运行的线程中都存在检查已修改文件的逻辑

以下是相同的代码:

import threading
import os
import time
from subprocess import Popen, PIPE

def getLatestModifiedFiles(seconds, _dir):
    files = (fle for rt, _, f in os.walk(_dir) for fle in f if time.time() - os.stat(
    os.path.join(rt, fle)).st_mtime < 300)
    return list(files)

def getLatestModifiedFilesUnix(seconds, _dir):
    lastseconds = seconds * -1
    p = Popen(['/usr/bin/find', _dir, '-mmin', str(lastseconds)], stdout=PIPE, stderr=PIPE)
    out, err = p.communicate()
    print out.strip("\r\n")
    if err != "":
        print err

def run(logPath):
    threadName = threading.currentThread().getName()
    getLatestModifiedFilesUnix(10, logPath)
    #files = getLatestModifiedFiles(300,logPath)
    #for file in files:
     #   print "message from %(ThreadName)s...%(File)s" % {'ThreadName': threadName, 'File' : file}


if __name__ == "__main__":
    logPaths = ["/home/appmyser/Rough", "/home/appmyser/Rough2"]
    threads = []
    for path in logPaths:
        t = Thread(target=run, args=(path,))
        threads.append(t)
        t.start()

    for t in threads:
        t.join()
导入线程
导入操作系统
导入时间
从子流程导入Popen、PIPE
def GetLatestModifiedFile(秒,目录):
files=(fle表示rt,u,f在os.walk(_dir)中表示fle在f if time.time()中-os.stat(
os.path.join(rt,fle)).st_mtime<300)
返回列表(文件)
def GetLatestModifiedFileUnix(秒,_dir):
lastseconds=秒*-1
p=Popen(['/usr/bin/find',_dir'-mmin',str(lastseconds)],stdout=PIPE,stderr=PIPE)
out,err=p.communicate()
打印输出.strip(“\r\n”)
如果出错!="":
打印错误
def运行(日志路径):
threadName=threading.currentThread().getName()
GetLatestModifiedFileUnix(10,日志路径)
#files=getLatestModifiedFile(300,日志路径)
#对于文件中的文件:
#打印“来自%(ThreadName)s..%(文件)s的消息”%%{'ThreadName':ThreadName',File':File}
如果名称=“\uuuuu main\uuuuuuuu”:
日志路径=[“/home/appmyser/Rough”,“/home/appmyser/Rough2”]
线程=[]
对于日志路径中的路径:
t=线程(目标=运行,参数=(路径,))
threads.append(t)
t、 开始()
对于螺纹中的t:
t、 加入
函数:
getLatestModifiedFiles
使用本机Python代码查找最新修改的文件;另一方面,函数:
getlatestmodifiedfileunix
使用unix find命令执行相同的操作

在第二种情况下,我使用子流程,据我所知,它创建了一个新流程。 我的问题是,从线程中调用子流程是一种好的做法吗?有什么后果我应该考虑吗?< / P > 另外,新创建的子流程的父流程是什么?有人能详细地告诉我它是如何工作的吗


非常感谢。

从后面开始。多线程进程仍然只是一个进程。不管哪个线程分叉(并执行),该进程都是新生成的子线程的父线程。您可以在系统上运行
ps-efL
,查看一下。如果您有任何正在运行的多线程应用程序(很可能),您将看到单个线程及其自己的轻量级进程标识共享一个进程id

至于后果。当使用
子流程时
,确实不应该因为这样做而感到意外。在较低级别中,如果您分叉并有多个线程,则需要注意,因为新创建的进程将只有一个(调用)线程,这可能会带来各种各样的乐趣,例如,如果它依赖于其他线程来释放锁。但由于此后不久就运行了exec,所以无论如何都要运行新代码


这不是您直接提出的问题,但我反对调用
find
获取目录列表。我宁愿在“内部”处理


同样在
getLatestModifiedFiles
中的生成器上,为每次比较调用
time.time()
,不仅成本更高,而且有效地意味着目标帖子的移动取决于每个项目的轮到处理时间。

非常感谢。