Chapter 11. Services, Processes, and Logging
Once you’ve built an application that behaves the way you want, the next step is to think about how you’re going to deploy it. How will you start and stop the process? Under which user account should it run? How will you keep track of what’s going on? Twisted provides a number of tools that you can use to manage how your application behaves in these areas. You can run your application in a background process, and limit its permissions by having it run as a specific user. You can run multiple services in the same process. And you can use logging to record errors and track your program’s usage.
Running a Twisted Application as a Daemon
Just as servers are different from regular applications, server processes are different from regular processes. A server needs to be available all the time, to all users, so it can’t be tied to one user’s session. Server processes run in the background and are usually started and stopped automatically by the operating system. In Unix and Unix-like operating systems, such long-running background processes are traditionally knows as daemons.
Twisted comes with a
program called twistd that you can use to run your
application as a background process. twistd can
also help your program to do other things you’d expect from a well-behaved server: write
log messages to the system log, run in a chroot
ed
environment where it has limited directory access, and run with the permissions of an
unprivileged user.
Tip
For more ...
Get Twisted Network Programming Essentials now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.