【问题标题】:ADB Python-Daemon Subprocess not connectingADB Python-Daemon 子进程未连接
【发布时间】:2020-09-22 20:02:30
【问题描述】:

我有一个简单的 python 守护程序,它在执行我的主要测试时在后台运行。这段代码在我的 Ubuntu 机器上运行良好,但自从在我的 Mac 上尝试后,我无法让它工作。

    #! /usr/bin/env python

import daemon
import time as t
import subprocess


def logging():
    while True:
        n = str(10)
        m = str(1)
        i = t.time()
        cpu = open("filepath/to/file" + str(i) + ".txt", "w")
        ram = open("filepath/to/file" + str(i) + ".txt", "w")
        disk = open("filepath/to/file", "a")
        subprocess.call(['adb', 'shell', 'top', '-m', n, '-n', m], stdout=cpu, stderr=cpu)
        subprocess.call(['adb', 'shell', 'cat /proc/meminfo'], stdout=ram)
        subprocess.call(['adb', 'shell', 'df', '/data'], stdout=disk)


def run():
    with daemon.DaemonContext():
        logging()


if __name__ == "__main__":
    run()

每当我执行此代码时,stderr 都会给我以下输出:

* daemon not running; starting now at tcp:5037
ADB server didn't ACK
Full server startup log: /var/folders/4_/_dcrxz611mv09n6nd404kj_80000gn/T//adb.501.log
Server had pid: 7910
--- adb starting (pid 7910) ---
adb I 06-03 12:32:24  7910 621421 main.cpp:62] Android Debug Bridge version 1.0.41
adb I 06-03 12:32:24  7910 621421 main.cpp:62] Version 30.0.1-6435776
adb I 06-03 12:32:24  7910 621421 main.cpp:62] Installed as /Users/dishbusiness/Desktop/Android/sdk/platform-tools/adb
adb I 06-03 12:32:24  7910 621421 main.cpp:62] 
adb F 06-03 12:32:25  7910 621421 main.cpp:153] could not install *smartsocket* listener: Address already in use

* failed to start daemon
adb: cannot connect to daemon

我可以使用 adb 连接到我的设备并运行我的主要测试。这个守护进程似乎只是不想在 Mac 上使用 adb。

  • python 版本 - 3.8.3
  • adb 版本 - 1.0.41
  • SDK 版本 - 30.0.1-6435776
  • Mac 操作系统 - 10.15.5

感谢任何帮助!

【问题讨论】:

    标签: python macos subprocess adb python-daemon


    【解决方案1】:

    我能够找到一种使用线程而不是守护程序的解决方法。代码见下文。

    import subprocess
    import os
    from threading import Thread
    from datetime import datetime
    
    
    def run():
        while True:
            m = str(1)
            now = datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
            cpu = open("/path/to/file" + str(now) + ".txt", "w")
            ram = open("/path/to/file" + str(now) + ".txt", "w")
            disk = open("/path/to/file/DISK.txt", "a")
            subprocess.call(['adb', 'shell', 'top', '-n', m], stdout=cpu, stderr=cpu)
            subprocess.call(['adb', 'shell', 'cat /proc/meminfo'], stdout=ram)
            subprocess.call(['adb', 'shell', 'df', '/data'], stdout=disk)
    
    
    def run2():
        subprocess.call(['pytest', 'file.py', '-v', '-s'])
        os._exit(1)
    
    
    if __name__ == "__main__":
        t1 = Thread(target=run)
        t2 = Thread(target=run2)
        t1.setDaemon(True)
        t2.setDaemon(True)
        t1.start()
        t2.start()
        while True:
            pass
    

    这个线程使用多线程让“后台”进程(运行)在后台运行,而主 pytest 进程运行(run2)。然后当 pytest 进程结束时,我使用os._exit(1) 杀死后台进程(运行)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-11
      • 1970-01-01
      • 2017-03-07
      相关资源
      最近更新 更多