Logging data from a subprocess (python,linux) -
i'm running 2 scripts in parallel follows:
import subprocess time import sleep subprocess.popen(["python3", 'tsn.py']) subprocess.popen(["python3", 'lsn.py'])
the above code in file called multi.py
both 'tsn.py' , 'lsn.py' logging data separate text files using file.write(). if run .py files individually log data fine, when run multi.py data logged prints on screen fine, doesn't logged in text files (i.e file.write() doesn't execute ). issue , how work around that? thanks.
edit: lsn.py looks this. tsn.py same
from socket import * import time servername_sen = '192.168.0.151' serverport_sen = 8080 clientsocket_sen = socket(af_inet,sock_stream) clientsocket_sen.connect((servername_sen,serverport_sen)) get='getd' status='off' logvar = 0 file = open('lsn_log.txt', 'a') while 1: time.sleep(0.5) clientsocket_sen.send(get.encode('utf-8')) print('lsn bp1') #print("get sent") num = clientsocket_sen.recv(1024) test=int(num) print("data received lsn:") print(test) if test>210: if status=='on': #clientsocket_act.send(off.encode('utf-8')) status='off' elif test<100: if status=='off': #clientsocket_act.send(on.encode('utf-8')) status='on' #the above code grabs data server #the code below causing issue logvar = logvar+1 if logvar == 5: print("bp2 lsn") file.write(time.strftime("%i:%m:%s")) file.write(" ") file.write(time.strftime("%d/%m/%y")) file.write(" ") file.write("the lights are: ") file.write(status) file.write(" ") #file.write(volt) file.write("\n") logvar=0
you need close files or let with
you:
with open('lsn_log.txt', 'a') f: while 1: time.sleep(0.5) clientsocket_sen.send(get.encode('utf-8')) print('lsn bp1') num = clientsocket_sen.recv(1024) test = int(num) print("data received lsn:") print(test) if test > 210: if status == 'on': #clientsocket_act.send(off.encode('utf-8')) status = 'off' elif test < 100: if status == 'off': #clientsocket_act.send(on.encode('utf-8')) status = 'on' logvar += 1 if logvar == 5: print("bp2 lsn") f.write("{} {} lights are: {}\n".format(time.strftime("%i:%m:%s"), time.strftime("%d/%m/%y"), status))
Comments
Post a Comment