forked from arskom/spyne
/
async.py
65 lines (54 loc) · 2.22 KB
/
async.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python
#
# soaplib - Copyright (C) 2009 Aaron Bickell, Jamie Kirkpatrick
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
#
import time
from threading import Thread
from soaplib.core.service import soap, DefinitionBase
from soaplib.core.model.primitive import String, Integer
from soaplib.core.util import get_callback_info
from soaplib.core.server import wsgi
from soaplib.core import Application
'''
This is a very simple async service that sleeps for a specified
number of seconds and then call back the caller with a message.
This kicks off a new Thread for each request, which is not recommended
for a real-world application. Soaplib does not provide any thread
management or scheduling mechanism, the service is responsible for the
execution of the async. process.
'''
class SleepingService(DefinitionBase):
@soap(Integer, _is_async=True)
def sleep(self, seconds):
msgid, replyto = get_callback_info()
def run():
time.sleep(seconds)
client = make_service_client(replyto, self)
client.woke_up('good morning', msgid=msgid)
Thread(target=run).start()
@soap(String, _is_callback=True)
def woke_up(self, message):
pass
if __name__=='__main__':
try:
from wsgiref.simple_server import make_server
soap_app = Application([SleepingService],'tns')
wsgi_app = wsgi.Application(soap_app)
server = make_server('localhost', 7789, wsgi_app)
server.serve_forever()
except ImportError:
print "Error: example server code requires Python >= 2.5"