Ticket #2692: test1.py

File test1.py, 721 bytes (added by yaubi, 15 years ago)

1st test, with signals

Line 
1#!/usr/bin/python
2
3import os, signal, time
4from twisted.internet import reactor, defer
5
6def terminate():
7    print "terminating ..."
8    d = defer.Deferred()
9    # do things that lasts long (eg. database, network, etc.)
10    reactor.callLater(1, d.callback, None)
11    return d
12
13pid = os.getpid()
14if os.fork():
15    # parent
16    reactor.addSystemEventTrigger('before', 'shutdown', terminate)
17    reactor.run()
18else:
19    time.sleep(0.5)
20    os.kill(pid, signal.SIGTERM)
21    time.sleep(0.5)
22    os.kill(pid, signal.SIGTERM)
23
24
25# Output without patch:
26# 1. 'terminate' method is called twice
27"""
28terminating...
29terminating...
30"""
31
32# Output with patch:
33# 1. 'terminate' method is called once, as expected
34"""
35terminating...
36"""
37