In this video, learn how to create a Windows service written in Python. See one way to correctly install the required pywin32 module that works for Python 3.5, and how to successfully run the postinstall script. Then, run your scheduled task as a Windows Service.
- [Narrator] Next we will be creating a Windows Service in order to run our scheduled tasks from within this Windows Service. Running our scheduled tasks as a Windows Service makes it much more robust. For example, we can set it up to start up automatically and should the PC reboot during the night due to a Windows update for example, the service will start up automatically and our scheduled task will still run. In order to create a Windows Service in Python we will need to import those three modules.
All these three modules are part of PyWin32 extension. Now installing third party extension, Python is usually very straight forward, we just install and off we go. But in this case it turns out to be a little bit more tricky. One way to install a Py Win32 for Python 3.5 that works, is to go to the gohike website and download the appropriate wheel installer and for Python 3.5 or 64 bit OS, it's this one.
One thing to notice is this sentence here underneath the installer package. To install PyWin32 system files, run Python exe scripts, PyWin32, host installer Py install from an elevated command prompt. That means we need to be local administrator. So usually we don't have to run a post install after installing a Python installer, but in this case we have to because this post install copies require dll's into the system pass or system files of Windows and result that it will work.
So after we've successfully installed PyWin32, we want to run the post install script. So the location is here, in my case Python 3.5 scripts and the PyWin32 post install script is in this folder. It's this one here. This is what we have to run. And now what's important is these open fields where we have the run installation. When it's successful it should look like this.
So I ran it already earlier on, so I run the post install and what's important is it's copying some dll's to Windows system 32, so it's copying pythoncom35 and pywintypes35 to the Windows system folder. And when those dll's are up there, then PyWin32 won't work at all. After we've successfully installed PyWin32 and ran the post install, we can now create our Python Windows Service.
Create a class here which inherits from Win32 service framework. Which is part of Win32 of PyWin32. Comes from here and then we basically override a few values. The underscore service name is very important, this is what shows up in task manager. And this is how we can net stop and net start a service. And in the net you have to initialize the superclass first and then here we created a vent that's a stop event.
So later we'll be in an endless loop and when we stop the service we'll get out of the loop because this event will be signaled. So there are a couple of functions here, service to run, and what we've done here we have put our scheduling code into this service to run method. So we have our job here, you get the weather data, we order it, et cetera, then we send the email. And we still see it run every minute. And our endless loop now from before, you remember from early on, here in task scheduler we set wild true schedule run pending, sleep one, second.
So here this is being replaced by this part. This is part of the service to run method. So within here this is our endless loop. So the return code, as long as it's not the Win32 object, we will keep looping. So here we have schedule run pending and then we wait five seconds, 5,000 milliseconds, and keep on looping and looping. But we get a stop event, that means if you stopped the service, this code will run.
And it will basically get us out of this loop here. And we won't be scheduling our events, we won't be emailing anymore, it's stopped. And down here there's also important part of the whole code and the command line and we have to pass in the name of the class Python Task Service, so that's the name of our class here. So this one, it should be passing at the bottom. When we run it, it looks like we have the main section here. Looks like it says command line, sounds like we could just run it.
So when we run it, it's not too happy. It tells us, usage task scheduler service py install update remove start stop restart debug. So what that tells us here is we can't run it by itself as a module, this needs to be installed as a Windows Service. Now one thing to notice is that we install it as a Windows Service, what we're doing is we do Python and then the name of the module, that's how we install it.
So that's not the name of the class, it could be, but it doesn't have to be. This is important we install the service, using Python, the name of the module, install, remove it by doing remove instead. The other important thing though is the service name, this is very important. Again, this we can net start and net stop by this name. Can see this name in task manager and should we change this name, then suddenly trying to uninstall, trying to remove here, won't work anymore.
So once we've installed the name of the module and the service name have to remain the same. So we go over here, you can see our module is called taskschedulerservice.py. We can now install our Python service by opening a command prompt and seeding to the location of the folder where this module is located. So then we can do install it, so Python, the name of the module, install, press Enter.
Says the service was installed. We now go to service manager and refresh. Under P, we have Python Task Scheduling Service set to manual by default and it's not running right now. We go to task manager. And refresh so we have to close it, reopen it, try again. So here we can see the name is Python Task SVC and Python Task Scheduling Service is under description, while in service manager, service control manager, SCM, the name is Python Task Scheduling Service, it shows it under the description.
And the description is what we thought would be the description of this Python Service Scheduled Tasks. So this comes from what we declared up here, service name, the display name, and the description. So we can see that there's a slight discrepancy between task manager and service control manager. Anyway, let's start our service with other code. It's running and now what it does is it will email us as before every one minute.
And if we wait long enough, we should get more emails every one minute. So that's basically it. In order to stop it, you just click the Stop button. One of the nice things is once the service is stopped, we can make changes. Let's say we want to change from one minute to one hour, which is what we originally had in mind. So now all we have to do is make this change, save it, and when we now start the service again, it will look at the latest version, at our changed version, and from now on our schedule will be every one hour instead of every one minute.
Note: This course was created by Packt Publishing. We are pleased to host this training in our library.
- Running Python programs within Eclipse
- Setting breakpoints
- Using the PyDev debugger
- Using design patterns
- Creating a GUI application with Tkinter
- Writing a Windows scheduling service