Calling a service . py inside another . py

Asked

Viewed 151 times

0

Good afternoon, you guys!

I researched a lot on the internet and did not succeed, I have the following problem to solve:

  • I have 3 Python services installed on Windows(example1.py / example2.py / example3.py) running normally, but the service only collects information once a month, but runs every 24 hours, on a specific day, 2 and 3 are daily and run every 3 hours.

  • I want to create another service that monitors the 3 created, that start at the exact date or once a day and after completion of the routine close completely, starting only the next day or at a specific date.

My 3 services start as follows:

class TestService(win32serviceutil.ServiceFramework):
    _svc_name_ = 'exemplo1'
    _svc_display_name_ = 'exemplo1'    

    def __init__(self, args):
        win32serviceutil.ServiceFramework.__init__(self, args)
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)        
        socket.setdefaulttimeout(60)

    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        win32event.SetEvent(self.hWaitStop)

    def SvcDoRun(self):
        servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE, servicemanager.PYS_SERVICE_STARTED, (self._svc_name_, ''))        
        self.main()


    def main(self):

        def retorna_data_00(dt):
            midnight = datetime.time(0)
            return datetime.datetime.combine(dt.date(), midnight)

        def data_modificacao(FileName):
            t = os.path.getmtime(FileName)
            return datetime.date.fromtimestamp(t)

And it closes as follows:

if __name__ == '__main__':
    if len(sys.argv) == 1:
        servicemanager.Initialize()
        servicemanager.PrepareToHostSingle(TestService)
        servicemanager.StartServiceCtrlDispatcher()
    else:
        win32serviceutil.HandleCommandLine(TestService)

Among these functions is the entire script that collects information from the bank, transforms it into a spreadsheet and sends it by email.

  • When Voce says he wants a service that monitors others, I imagine it’s something that shows if everyone is running and can point to a possible correct failure?

  • In fact also, I need the routine to make the points mentioned above and perform the start in the services to and stop when completed.

2 answers

0


I found a way by exporting my other script as . py and calling it as library within the current script, example:

try:
   import scriptantigo
except Exception as e:
    print(e)

In this way it worked!

0

Good being so we have two observations, one of which a windows service does not call another service, windows service is, give that do not occur problems, always running.

Given this, some things would have to be changed in the structure, for example by just creating a service that calls others .py which would be independent files, for an approach like this, or similar you can use the subprocess.

But the approach I suggest is not to use windows services, instead to use a solution with Docker containers with an orchestrator, this would be the approach closer to what you want, and the orchestrator would do the monitoring part and other tasks of the kind. If you choose to go this way this documentation will greatly help

A third approach is still to communicate these services through a database, but I do not recommend.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.