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

问题描述 投票:1回答:1

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

下面是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')
c# python logging raspberry-pi process.start
1个回答
0
投票

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

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

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

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

HF

© www.soinside.com 2019 - 2024. All rights reserved.