在python多处理中从bash调用另一个应用程序非常慢

问题描述 投票:2回答:1

我正在尝试使用qark分析器来分析使用python进行多处理的一组apks。

试图分析一组100个apks,我发现我写的自动化分析的应用程序非常慢。最后的分析我跑了大约20个小时,然后我手动关闭了我的电脑,因为它已经变得无法使用,可能是由于RAM使用量很大......分析甚至有害,弄乱我的Windows分区并阻止我看到分区内的数据和Windows再次启动(我从ubuntu运行分析,但是进入我的Windows分区以获得可用的磁盘空间)

在这个过程中执行的类的核心是非常相似的

 def scanApk(self):

    try:

        #Creating a directory for qark build files (decompiled sources etc...)
        buildDirectoryPath = os.path.join(os.path.join(self.APKANALYSIS_ROOT_DIRECTORY, "qarkApkBuilds"), "build_" + self.apkInfo["package_name"])

        os.mkdir(buildDirectoryPath)

        start = timer()

        subp = subprocess.Popen(self.binPath + "/qark --report-type json --apk \"" + self.apkPath + "\"", cwd=buildDirectoryPath, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
            preexec_fn=os.setsid)

        #Setting a timeout of 6 hours for the analysis
        out, err = subp.communicate(timeout= 6 * (60 * 60))

        self.saveOutAndErr(out, err)


        if subp.returncode != 0:

            raise subprocess.CalledProcessError(subp.returncode, "qark")

        self.printAnalysisLasting(start)


        #Moving qark report into qark reports collecting directory
        subp = subprocess.Popen("mv \"" + self.defaultReportsPath + "/" + self.apkInfo["package_name"] + ".json\" " + "\"" + self.toolReportsDirectory + "\"", shell=True)

        out, err = subp.communicate()


        if subp.returncode != 0:

            raise subprocess.CalledProcessError(subp.returncode, "qark")


        return True

[... subprocess.TimeoutExpired and subprocess.CalledProcessError exceptions handling...]

我在使用concurrent.futures的ProcessPoolExecutor这样的多处理中使用类(scanApk方法在analyzeApk方法中调用):

with concurrent.futures.ProcessPoolExecutor(max_workers = 10) as executor:

        futuresList = []

        #Submitting tasks to ProcessPoolExecutor

        for apkPath in apksToAnalyzePaths:

            ...

            qarkAnalyzer = QarkAnalyzer(...)

            futuresList.append(executor.submit(qarkAnalyzer.analyzeApk))


        for future in futuresList:

            future.result()

相反,这是htop显示的2个apks分析过程中进程状态的快照:

enter image description here

我测试了应用程序,分析了2个apks,它看起来表现得很“漂亮”......我经历了qark apk分析执行时间相对于该apk上单个分析执行的增加,但我把它归结为多处理,看到它不是太多,我认为它可能没问题......但是对于100次执行,执行导致了灾难。

有人可以帮助找出这里发生的事情吗?为什么分析这么慢?怎么会弄乱我的Windows分区? RAM内存电量太重,无法分析这么多的apks?这是由于我的应用程序中的进程使用不当?我该怎么办呢?

python multiprocessing concurrent.futures
1个回答
0
投票

你的Windows分区可能发生的事情是qark的输出JSON文件写在磁盘的某个重要区域,破坏了一些数据结构,如MFT(如果你使用NTFS)。

在您的代码中,您生成10个工作线程。这些都是内存和处理密集型线程。除非你有超过10个内核,否则这将消耗你所有的处理能力,触发超线程(如果可用)并使系统变得太慢。

要从系统中获得最大性能,您必须为每个工作核心运行一个线程。为此,请运行:

with concurrent.futures.ProcessPoolExecutor(max_workers = os.cpu_count()) as executor:

    futuresList = []

                             . . .

另一个问题是static analysis is known to cause problems with qark

最后,请注意100个apks是一个很大的负载。预计需要一段时间。如果资源被过度请求,竞争条件可能导致性能比分配的资源更少。你应该调整你的处理甚至memory usage

© www.soinside.com 2019 - 2024. All rights reserved.