using twistd to run a twisted application but script run twice

184 Views Asked by At

sample code here

# main.py
from twisted.application import service, internet

application = service.Application("x")
service.IProcess(application).processName = "x"

print "some log...."

if I run this main.py with:

twistd -y main.py

I got 2 "some log...." lines.

If this code run twice?

enter image description here

2

There are 2 best solutions below

1
On BEST ANSWER

The "process name" feature you're using works by re-executing the process with a new argv[0]. There is no completely reliable way to save an arbitrary object (like the Application) across this process re-execution. This means that the .py file has to be re-evaluated in the new process to recreate the Application object so twistd knows what you want it to do.

0
On

You might want to consider using setproctitle rather than twistd's built-in process title feature. (For that matter, maybe twistd should just use it if it's available...)