首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ADB Python-Daemon子进程没有连接

ADB Python-Daemon子进程没有连接
EN

Stack Overflow用户
提问于 2020-06-03 23:30:28
回答 1查看 307关注 0票数 0

我有一个简单的python守护进程,它将在执行我的主要测试时在后台运行。这段代码在我的Ubuntu盒上运行得很好,但是由于在我的Mac上试用了它,所以我无法让它工作。

代码语言:javascript
复制
    #! /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就会给出以下输出:

代码语言:javascript
复制
* 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

我能够连接到我的设备与亚行和运行我的主要测试。这个守护进程似乎不想在Mac上与亚行合作。

30.0.1-6435776

  • Mac

  • python版本- 3.8.3

  • adb版本- 1.0.41

  • SDK版本-

  • OS - 10.15.5

任何帮助都是非常感谢的!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-12 17:45:55

我找到了一项利用线程而不是守护进程的工作。有关代码,请参阅下面。

代码语言:javascript
复制
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

此线程使用多线程在后台运行“后台”进程(run),而主pytest进程运行(run2)。然后,当pytest进程结束时,我使用os._exit(1)来终止后台进程(运行)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62184420

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档