I have a simple python-daemon that is to run in the background while my main test(s) are executed. This code worked fine on my Ubuntu box, but since trying it on my Mac I am unable to get it to work.
#! /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()
Whenever I execute this code the stderr gives me the following output:
* 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
I am able to connect to my devices with adb and run my main test. It just seems to be something with this daemon that does not want to work with adb on Mac.
Any help is appreciated!
I was able to figure out a work around that utilizes Threading instead of a daemon. See below for the code.
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
This Thread uses multi-threading to have both the "background" process (run) running in the background while the main pytest process runs (run2). Then when the pytest process ends I use os._exit(1)
to kill the background process (run).