2

作成しようとしている Windows システム サービスがあります。私は POS マシンのインターフェイスを作成しようとしているので、理想的にはシステム サービス内にこのコードを含めたいと考えています。ただし、いくつかの実験により、Windows システム サービスは基本的なタスクのみを実行し、他の反復は実行しないと考えるようになりました。

x 秒ごとに呼び出す必要がある別の関数があります。この追加関数は while ループですが、関数と win32 ループがシステム コールがうまく連携するのを待つことができません。以下のコードで詳しく説明します。

import win32service  
import win32serviceutil  
import win32event

class PySvc(win32serviceutil.ServiceFramework):  
    # net name  
    _svc_name_ = "test"  

    _svc_display_name_ = "test"  

    _svc_description_ = "Protects your computer."  

    def __init__(self, args):  
        win32serviceutil.ServiceFramework.__init__(self,args)  
        # create an event to listen for stop requests on  
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)


    # core logic of the service     
    def SvcDoRun(self):


        # if the stop event hasn't been fired keep looping
        while rc != win32event.WAIT_OBJECT_0:




            # block for 60 seconds and listen for a stop event  
            rc = win32event.WaitForSingleObject(self.hWaitStop, 60000)

        ## I want to put an additional function that uses a while loop here.
        ## The service will not work correctly with additional iterations, inside or 
        ## the above api calls.    
        ## Due to the nature of the service and the api call above, 
        ## this leads me to have to compile an additional .exe and somehow call that 
        ## from the service.     

    # called when we're being shut down      

    def SvcStop(self):  
            # tell the SCM we're shutting down  
            self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)  
            # fire the stop event  
            win32event.SetEvent(self.hWaitStop)  

if __name__ == '__main__':  

    win32serviceutil.HandleCommandLine(PySvc) 

私の調査によると、何らかの方法で Windows システム サービスから .exe を呼び出す必要があることがわかりました。誰もこれを行う方法を知っていますか? 私は os.system を使用しようとしましたが、サブプロセス モジュールのバリアント コールは役に立ちませんでした。Windows は単にそれらを無視しているようです。何か案は?

編集:元の質問に戻る

4

1 に答える 1

0

Can't say as I'm familiar with Windows development but in *nix I've found sockets are very useful in situations where two things shouldn't be able to talk by definition but you need them to anyway e.g. making web browsers launch desktop apps, making the clipboard interact with the browser etc.

In most cases UDP sockets are all that you need for a little IPC and they are trivial to code for in Python. You do have to be extra careful though, often restrictions are there for a good reason and you need to really understand a rule before you go breaking it... Bear in mind anyone can send a UDP packet so make sure the receiving app only accept packets from localhost and make sure you sanity check all incoming packets to protect against local hackers/malware. If the data transmitted is particularly sensitive or the action initiated is powerful it may not be a good idea at all, only you know your app well enough to say really.

于 2012-09-11T19:06:03.347 に答える