Можно ли распечатать файл, если сценарий python установлен как служба windows с NSSM? - PullRequest
0 голосов
/ 06 мая 2020

У меня есть эта функция в сценарии Python, который был установлен как служба windows с NSSM. Когда сценарий Python выполняется в обычном режиме (т.е. не как служба), файл печатается. Однако, когда он установлен как служба, он не распечатывается.

Печать запускается извне через op c -ua.

Можно ли вообще печатать, пока скрипт установлен как служба? При установке в качестве службы у меня тоже нет исключений. Иногда я получал исключение KeyboardInterrupt во время сна между циклами.

def CheckOpcuaNode(latestPDF):
    client = Client("opc.tcp://192.168.202.90:4840/")
    try:
        client.connect()
        opcuaNode = client.get_node("ns=6;s=::AsGlobalPV:g_saveParameters.bPrintNow")
        Result = opcuaNode.get_value() 
        if Result == True:
             print("print file: %s" % str(latestPDF))
             os.startfile(latestPDF, "print")
             try:
                time.sleep(4)
             except KeyboardInterrupt:  # Ignore keyborditerruption
                print("ERROR KeyboardInterrupt while printing: %s" % sys.exc_info()[0])
             opcuaNode.set_attribute(ua.AttributeIds.Value, ua.DataValue(False))
    except:
            print("ERROR while checking if PDF shall be printed: %s" % sys.exc_info()[0])
    finally:
        client.disconnect()

def LatestPdf():
    return 'path\to\PDF\file.pdf'

if __name__ == '__main__':
while True:
    latestPdfFile = LatestPdf()
    if latestPdfFile != '':
        CheckOpcuaNode(latestPdfFile) # check If PLC has asked to print pdf file
    try:
        time.sleep(4)
    except KeyboardInterrupt:  # Ignore keyborditerruption
        print("ERROR KeyboardInterrupt between loops: %s" % sys.exc_info()[0])
...