0

I'm running two scripts in parallel as follows:

import subprocess
from time import sleep
subprocess.Popen(["python3", 'tsn.py'])
subprocess.Popen(["python3", 'lsn.py'])

The above code is in a file called multi.py

Both 'tsn.py' and 'lsn.py' were logging data to separate text files using file.write(). If I run the .py files individually they log data just fine, however when I run multi.py the data to be logged prints on my screen just fine, but it doesn't get logged in the text files (i.e file.write() doesn't execute ). What is the issue and how do I work around that? Thanks.

EDIT: lsn.py looks like this. tsn.py is almost exactly the 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 from 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 simply grabs data from a server


#THE CODE BELOW IS WHAT IS CAUSING THE 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
10
  • what do tsn and lsn look like? Also why not just redirect the stdout from each process to a file? Commented Mar 21, 2015 at 10:14
  • @PadraicCunningham I hope to eventually remove all print's in my final program. In any case, I still don't know how to redirect stdout to a file. Could you tell me how it's done? I want to store the output from both scripts to separate text files. Commented Mar 21, 2015 at 10:40
  • I will add a subprocess example. Your problem right now is more than likely not closing your files, try adding file.close() Commented Mar 21, 2015 at 10:41
  • @PadraicCunningham The program is supposed to run indefinitely, but I'll try adding file.close and see if it logs the data at least once. Commented Mar 21, 2015 at 10:48
  • @you are writing to two different files yes? Commented Mar 21, 2015 at 10:50

1 Answer 1

1

You need to close your files or let with do it for you:

with open('lsn_log.txt', 'a') as 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 from 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("{} {}  The Lights are:  {}\n".format(time.strftime("%I:%M:%S"), time.strftime("%d/%m/%Y"), status))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.