当我使用Process.Start运行python程序时,我的python程序中的日志不起作用?

时间:2021-02-25 20:45:34

I'm running a python program from c# using process.start() on a raspberry pi zero w. I've tested it the program is running when called but it will not log to file. But if I just run the program myself python Relays.py 0 0 0 0 0 the python log works. Does anyone know what is causing this problem?

我在一个覆盆子pi零w上使用process.start()从c#运行一个python程序。我已经测试了程序在调用时正在运行,但它不会记录到文件。但是,如果我只是自己运行程序python Relays.py 0 0 0 0 0 python日志工作。有谁知道导致这个问题的原因是什么?

Below are the python code and C# function:

下面是python代码和C#函数:

void update()
        {
            String acommand = status();
            Console.WriteLine("LOOKSLIKEWEMADEIT " + acommand);
            String location = "/home/pi/Debug/Relays.py " + Convert.ToString(acommand);
            //run python 
            ProcessStartInfo info = new ProcessStartInfo("python", location);
            Process.Start(info);
            Console.WriteLine("YOUHAVEPASSEd");

        }

import RPi.GPIO as G
import time
import sys
import socket
import threading
import logging

G.setwarnings(False)
G.setmode(G.BOARD)

relays = (24,26,32,36,38)

for r in relays:
 G.setup(r,G.OUT)

logging.basicConfig(filename='Relaytrigger.log',level=logging.DEBUG,format='%(asctime)s %(message)s$

logging.info('\r\n')
logging.info(sys.argv)
logging.info('\r\n')

#######################heat
if sys.argv[1] == '1':
 heater = True
 G.output(relays[0],1)
 logging.info('heaton\r\n')
else:
 heater = False
 G.output(relays[0],0)
 logging.info('heatoff\r\n')
##########################main
if sys.argv[2] == '1':
 mainpump = True
 G.output(relays[1],1)
 logging.info('mainon\r\n')
else:
 mainpump = False
 G.output(relays[1],0)
 logging.info('mainoff\r\n')
#########################aux
if sys.argv[3] == '1':
 auxilarypump = True
 G.output(relays[2],1)
 logging.info('auxon\r\n')
else:
 auxilarypump = False
 G.output(relays[2],0)
 logging.info('auxoff\r\n')
#########################auxset
if sys.argv[4] == '1':
 auxsetting = True
 G.output(relays[3],1)
 logging.info('mainhigh\r\n')
else:
 auxsetting = False
 G.output(relays[3],0)
 logging.info('heatlow\r\n')
########################light
if sys.argv[5] == '1':
 lighting = True
 G.output(relays[4],1)
 logging.info('lighton\r\n')
else:
 lighting = False
 G.output(relays[4],0)
 logging.info('lightoff\r\n')

1 个解决方案

#1


0  

I seems to me like problems with your CWD (current working directory).

在我看来,我喜欢你的CWD(当前工作目录)的问题。

Depending on from where an python application is started "relative paths" may differ.

根据python应用程序的启动位置,“相对路径”可能不同。

Solution #1 : "The python-side solution": Use absolute paths! Relative paths are more likely to be unsafe.

解决方案#1:“python-side解决方案”:使用绝对路径!相对路径更可能不安全。

Solution #2 : "The C#-side solution": Use the ProcessStartInfo-Property "WorkingDirectory" to define your CWD. Your CWD is usualy the directory containing your .py file. Link to ProcessStartInfo-Class

解决方案#2:“C#-side解决方案”:使用ProcessStartInfo-Property“WorkingDirectory”来定义您的CWD。您的CWD通常是包含.py文件的目录。链接到ProcessStartInfo-Class

HF

#1


0  

I seems to me like problems with your CWD (current working directory).

在我看来,我喜欢你的CWD(当前工作目录)的问题。

Depending on from where an python application is started "relative paths" may differ.

根据python应用程序的启动位置,“相对路径”可能不同。

Solution #1 : "The python-side solution": Use absolute paths! Relative paths are more likely to be unsafe.

解决方案#1:“python-side解决方案”:使用绝对路径!相对路径更可能不安全。

Solution #2 : "The C#-side solution": Use the ProcessStartInfo-Property "WorkingDirectory" to define your CWD. Your CWD is usualy the directory containing your .py file. Link to ProcessStartInfo-Class

解决方案#2:“C#-side解决方案”:使用ProcessStartInfo-Property“WorkingDirectory”来定义您的CWD。您的CWD通常是包含.py文件的目录。链接到ProcessStartInfo-Class

HF