Mercurial > cosocket
annotate cosocket.py @ 23:1683a8fc76b0
Exception tracebacks in coroutines are now much more helpful.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Mon, 20 Apr 2009 07:22:57 -0700 |
parents | 425776983cf2 |
children | 7e3b3eb57ec2 |
rev | line source |
---|---|
23
1683a8fc76b0
Exception tracebacks in coroutines are now much more helpful.
Atul Varma <varmaa@toolness.com>
parents:
22
diff
changeset
|
1 import sys |
0 | 2 import socket |
1
f1159b9ec823
Initial implementation of coroutine-based server.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
3 import asyncore |
f1159b9ec823
Initial implementation of coroutine-based server.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
4 import asynchat |
12
1ffa6554ff3a
coroutines can now yield other coroutines, which are called in a nested manner.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
5 import types |
18
69efb6cdc127
Added some error-handling code, though the channel code is raising exceptions in it at the moment I think.
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
6 import traceback |
1
f1159b9ec823
Initial implementation of coroutine-based server.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
7 |
11
8574ff006a28
instructions are now objects.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
8 class _AsyncChatCoroutineDispatcher(asynchat.async_chat): |
2
f6f9cc0385be
Clients can now be coroutines too.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
9 def __init__(self, coroutine, conn = None): |
1
f1159b9ec823
Initial implementation of coroutine-based server.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
10 asynchat.async_chat.__init__(self, conn) |
f1159b9ec823
Initial implementation of coroutine-based server.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
11 self.set_terminator(None) |
2
f6f9cc0385be
Clients can now be coroutines too.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
12 self.__coroutine = coroutine |
1
f1159b9ec823
Initial implementation of coroutine-based server.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
13 self.__data = [] |
12
1ffa6554ff3a
coroutines can now yield other coroutines, which are called in a nested manner.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
14 self.__coroutine_stack = [] |
2
f6f9cc0385be
Clients can now be coroutines too.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
15 if conn: |
21
b77e679ce993
Renamed return_from_yield() to continue_from_yield().
Atul Varma <varmaa@toolness.com>
parents:
19
diff
changeset
|
16 self.continue_from_yield() |
1
f1159b9ec823
Initial implementation of coroutine-based server.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
17 |
22
425776983cf2
Removed the weakref mechanism from channels.py and instead turned until_message_received() into a generator rather than an object. Added a return_value() instruction to cosocket.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
18 def close_coroutine_and_return_to_caller(self, message): |
425776983cf2
Removed the weakref mechanism from channels.py and instead turned until_message_received() into a generator rather than an object. Added a return_value() instruction to cosocket.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
19 self.__close_coroutine(self.__coroutine) |
425776983cf2
Removed the weakref mechanism from channels.py and instead turned until_message_received() into a generator rather than an object. Added a return_value() instruction to cosocket.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
20 if self.__coroutine_stack: |
425776983cf2
Removed the weakref mechanism from channels.py and instead turned until_message_received() into a generator rather than an object. Added a return_value() instruction to cosocket.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
21 self.__coroutine = self.__coroutine_stack.pop() |
425776983cf2
Removed the weakref mechanism from channels.py and instead turned until_message_received() into a generator rather than an object. Added a return_value() instruction to cosocket.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
22 self.continue_from_yield(message) |
425776983cf2
Removed the weakref mechanism from channels.py and instead turned until_message_received() into a generator rather than an object. Added a return_value() instruction to cosocket.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
23 else: |
425776983cf2
Removed the weakref mechanism from channels.py and instead turned until_message_received() into a generator rather than an object. Added a return_value() instruction to cosocket.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
24 self.__coroutine = None |
425776983cf2
Removed the weakref mechanism from channels.py and instead turned until_message_received() into a generator rather than an object. Added a return_value() instruction to cosocket.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
25 |
23
1683a8fc76b0
Exception tracebacks in coroutines are now much more helpful.
Atul Varma <varmaa@toolness.com>
parents:
22
diff
changeset
|
26 def continue_from_yield(self, message = None, |
1683a8fc76b0
Exception tracebacks in coroutines are now much more helpful.
Atul Varma <varmaa@toolness.com>
parents:
22
diff
changeset
|
27 exception_info = None): |
1
f1159b9ec823
Initial implementation of coroutine-based server.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
28 try: |
23
1683a8fc76b0
Exception tracebacks in coroutines are now much more helpful.
Atul Varma <varmaa@toolness.com>
parents:
22
diff
changeset
|
29 if exception_info: |
1683a8fc76b0
Exception tracebacks in coroutines are now much more helpful.
Atul Varma <varmaa@toolness.com>
parents:
22
diff
changeset
|
30 instruction = self.__coroutine.throw(*exception_info) |
18
69efb6cdc127
Added some error-handling code, though the channel code is raising exceptions in it at the moment I think.
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
31 else: |
69efb6cdc127
Added some error-handling code, though the channel code is raising exceptions in it at the moment I think.
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
32 instruction = self.__coroutine.send(message) |
1
f1159b9ec823
Initial implementation of coroutine-based server.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
33 except StopIteration: |
12
1ffa6554ff3a
coroutines can now yield other coroutines, which are called in a nested manner.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
34 if self.__coroutine_stack: |
1ffa6554ff3a
coroutines can now yield other coroutines, which are called in a nested manner.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
35 self.__coroutine = self.__coroutine_stack.pop() |
21
b77e679ce993
Renamed return_from_yield() to continue_from_yield().
Atul Varma <varmaa@toolness.com>
parents:
19
diff
changeset
|
36 self.continue_from_yield() |
12
1ffa6554ff3a
coroutines can now yield other coroutines, which are called in a nested manner.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
37 else: |
18
69efb6cdc127
Added some error-handling code, though the channel code is raising exceptions in it at the moment I think.
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
38 self.__coroutine = None |
69efb6cdc127
Added some error-handling code, though the channel code is raising exceptions in it at the moment I think.
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
39 self.handle_close() |
69efb6cdc127
Added some error-handling code, though the channel code is raising exceptions in it at the moment I think.
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
40 except Exception, e: |
69efb6cdc127
Added some error-handling code, though the channel code is raising exceptions in it at the moment I think.
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
41 if self.__coroutine_stack: |
69efb6cdc127
Added some error-handling code, though the channel code is raising exceptions in it at the moment I think.
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
42 self.__coroutine = self.__coroutine_stack.pop() |
23
1683a8fc76b0
Exception tracebacks in coroutines are now much more helpful.
Atul Varma <varmaa@toolness.com>
parents:
22
diff
changeset
|
43 self.continue_from_yield(exception_info = sys.exc_info()) |
18
69efb6cdc127
Added some error-handling code, though the channel code is raising exceptions in it at the moment I think.
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
44 else: |
69efb6cdc127
Added some error-handling code, though the channel code is raising exceptions in it at the moment I think.
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
45 self.handle_error() |
11
8574ff006a28
instructions are now objects.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
46 else: |
12
1ffa6554ff3a
coroutines can now yield other coroutines, which are called in a nested manner.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
47 if type(instruction) == types.GeneratorType: |
1ffa6554ff3a
coroutines can now yield other coroutines, which are called in a nested manner.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
48 self.__coroutine_stack.append(self.__coroutine) |
1ffa6554ff3a
coroutines can now yield other coroutines, which are called in a nested manner.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
49 self.__coroutine = instruction |
21
b77e679ce993
Renamed return_from_yield() to continue_from_yield().
Atul Varma <varmaa@toolness.com>
parents:
19
diff
changeset
|
50 self.continue_from_yield() |
12
1ffa6554ff3a
coroutines can now yield other coroutines, which are called in a nested manner.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
51 else: |
1ffa6554ff3a
coroutines can now yield other coroutines, which are called in a nested manner.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
52 instruction.execute(self) |
1
f1159b9ec823
Initial implementation of coroutine-based server.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
53 |
18
69efb6cdc127
Added some error-handling code, though the channel code is raising exceptions in it at the moment I think.
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
54 def __close_coroutine(self, coroutine): |
69efb6cdc127
Added some error-handling code, though the channel code is raising exceptions in it at the moment I think.
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
55 try: |
69efb6cdc127
Added some error-handling code, though the channel code is raising exceptions in it at the moment I think.
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
56 coroutine.close() |
69efb6cdc127
Added some error-handling code, though the channel code is raising exceptions in it at the moment I think.
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
57 except Exception: |
69efb6cdc127
Added some error-handling code, though the channel code is raising exceptions in it at the moment I think.
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
58 self.log_info(traceback.format_exc(), 'error') |
69efb6cdc127
Added some error-handling code, though the channel code is raising exceptions in it at the moment I think.
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
59 |
69efb6cdc127
Added some error-handling code, though the channel code is raising exceptions in it at the moment I think.
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
60 def handle_close(self): |
69efb6cdc127
Added some error-handling code, though the channel code is raising exceptions in it at the moment I think.
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
61 if self.__coroutine: |
69efb6cdc127
Added some error-handling code, though the channel code is raising exceptions in it at the moment I think.
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
62 # Pass an exception back into the coroutine to kick |
69efb6cdc127
Added some error-handling code, though the channel code is raising exceptions in it at the moment I think.
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
63 # it out of whatever yielding state it's in. |
69efb6cdc127
Added some error-handling code, though the channel code is raising exceptions in it at the moment I think.
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
64 self.__close_coroutine(self.__coroutine) |
69efb6cdc127
Added some error-handling code, though the channel code is raising exceptions in it at the moment I think.
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
65 self.__coroutine = None |
69efb6cdc127
Added some error-handling code, though the channel code is raising exceptions in it at the moment I think.
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
66 while self.__coroutine_stack: |
69efb6cdc127
Added some error-handling code, though the channel code is raising exceptions in it at the moment I think.
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
67 self.__close_coroutine(self.__coroutine_stack.pop()) |
69efb6cdc127
Added some error-handling code, though the channel code is raising exceptions in it at the moment I think.
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
68 self.close() |
69efb6cdc127
Added some error-handling code, though the channel code is raising exceptions in it at the moment I think.
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
69 |
69efb6cdc127
Added some error-handling code, though the channel code is raising exceptions in it at the moment I think.
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
70 def log_info(self, message, type='info'): |
69efb6cdc127
Added some error-handling code, though the channel code is raising exceptions in it at the moment I think.
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
71 # TODO: Use the logging module here. |
69efb6cdc127
Added some error-handling code, though the channel code is raising exceptions in it at the moment I think.
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
72 print '%s: %s' % (type, message) |
69efb6cdc127
Added some error-handling code, though the channel code is raising exceptions in it at the moment I think.
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
73 |
69efb6cdc127
Added some error-handling code, though the channel code is raising exceptions in it at the moment I think.
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
74 def handle_error(self): |
69efb6cdc127
Added some error-handling code, though the channel code is raising exceptions in it at the moment I think.
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
75 self.log_info(traceback.format_exc(), 'error') |
69efb6cdc127
Added some error-handling code, though the channel code is raising exceptions in it at the moment I think.
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
76 |
2
f6f9cc0385be
Clients can now be coroutines too.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
77 def handle_connect(self): |
21
b77e679ce993
Renamed return_from_yield() to continue_from_yield().
Atul Varma <varmaa@toolness.com>
parents:
19
diff
changeset
|
78 self.continue_from_yield() |
2
f6f9cc0385be
Clients can now be coroutines too.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
79 |
1
f1159b9ec823
Initial implementation of coroutine-based server.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
80 def initiate_send(self): |
f1159b9ec823
Initial implementation of coroutine-based server.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
81 asynchat.async_chat.initiate_send(self) |
f1159b9ec823
Initial implementation of coroutine-based server.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
82 if ((not self.ac_out_buffer) and |
f1159b9ec823
Initial implementation of coroutine-based server.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
83 (len(self.producer_fifo) == 0) and |
f1159b9ec823
Initial implementation of coroutine-based server.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
84 self.connected): |
21
b77e679ce993
Renamed return_from_yield() to continue_from_yield().
Atul Varma <varmaa@toolness.com>
parents:
19
diff
changeset
|
85 self.continue_from_yield() |
1
f1159b9ec823
Initial implementation of coroutine-based server.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
86 |
f1159b9ec823
Initial implementation of coroutine-based server.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
87 def collect_incoming_data(self, data): |
f1159b9ec823
Initial implementation of coroutine-based server.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
88 # TODO: Enforce some maximum data length. |
f1159b9ec823
Initial implementation of coroutine-based server.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
89 self.__data.append(data) |
f1159b9ec823
Initial implementation of coroutine-based server.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
90 |
f1159b9ec823
Initial implementation of coroutine-based server.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
91 def found_terminator(self): |
f1159b9ec823
Initial implementation of coroutine-based server.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
92 self.set_terminator(None) |
f1159b9ec823
Initial implementation of coroutine-based server.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
93 data = ''.join(self.__data) |
4
4e6ce85226f4
Fixed a bug in receiving of data.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
94 self.__data = [] |
21
b77e679ce993
Renamed return_from_yield() to continue_from_yield().
Atul Varma <varmaa@toolness.com>
parents:
19
diff
changeset
|
95 self.continue_from_yield(data) |
1
f1159b9ec823
Initial implementation of coroutine-based server.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
96 |
7
d3ae3fc76711
Removed all use of the word 'Chatty'
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
97 class CoroutineSocketServer(asyncore.dispatcher): |
3
9e819377ce9f
Changed the way coroutine factories work.
Atul Varma <varmaa@toolness.com>
parents:
2
diff
changeset
|
98 def __init__(self, addr, coroutineFactory): |
9e819377ce9f
Changed the way coroutine factories work.
Atul Varma <varmaa@toolness.com>
parents:
2
diff
changeset
|
99 asyncore.dispatcher.__init__(self) |
9e819377ce9f
Changed the way coroutine factories work.
Atul Varma <varmaa@toolness.com>
parents:
2
diff
changeset
|
100 self.__coroutineFactory = coroutineFactory |
9e819377ce9f
Changed the way coroutine factories work.
Atul Varma <varmaa@toolness.com>
parents:
2
diff
changeset
|
101 self.create_socket(socket.AF_INET, socket.SOCK_STREAM) |
9e819377ce9f
Changed the way coroutine factories work.
Atul Varma <varmaa@toolness.com>
parents:
2
diff
changeset
|
102 self.set_reuse_addr() |
9e819377ce9f
Changed the way coroutine factories work.
Atul Varma <varmaa@toolness.com>
parents:
2
diff
changeset
|
103 self.bind(addr) |
9e819377ce9f
Changed the way coroutine factories work.
Atul Varma <varmaa@toolness.com>
parents:
2
diff
changeset
|
104 self.listen(1) |
9e819377ce9f
Changed the way coroutine factories work.
Atul Varma <varmaa@toolness.com>
parents:
2
diff
changeset
|
105 |
9e819377ce9f
Changed the way coroutine factories work.
Atul Varma <varmaa@toolness.com>
parents:
2
diff
changeset
|
106 def run(self): |
9e819377ce9f
Changed the way coroutine factories work.
Atul Varma <varmaa@toolness.com>
parents:
2
diff
changeset
|
107 while 1: |
9e819377ce9f
Changed the way coroutine factories work.
Atul Varma <varmaa@toolness.com>
parents:
2
diff
changeset
|
108 asyncore.loop(timeout = 0.5) |
9e819377ce9f
Changed the way coroutine factories work.
Atul Varma <varmaa@toolness.com>
parents:
2
diff
changeset
|
109 |
9e819377ce9f
Changed the way coroutine factories work.
Atul Varma <varmaa@toolness.com>
parents:
2
diff
changeset
|
110 def handle_accept(self): |
9e819377ce9f
Changed the way coroutine factories work.
Atul Varma <varmaa@toolness.com>
parents:
2
diff
changeset
|
111 conn, addr = self.accept() |
5
4ec829dc7d1d
Renamed ChatInstructions to 'to_send()', 'to_receive()' for readability.
Atul Varma <varmaa@toolness.com>
parents:
4
diff
changeset
|
112 coroutine = self.__coroutineFactory(addr) |
11
8574ff006a28
instructions are now objects.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
113 _AsyncChatCoroutineDispatcher(coroutine, conn) |
3
9e819377ce9f
Changed the way coroutine factories work.
Atul Varma <varmaa@toolness.com>
parents:
2
diff
changeset
|
114 |
11
8574ff006a28
instructions are now objects.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
115 class CoroutineSocketClient(_AsyncChatCoroutineDispatcher): |
3
9e819377ce9f
Changed the way coroutine factories work.
Atul Varma <varmaa@toolness.com>
parents:
2
diff
changeset
|
116 def __init__(self, addr, coroutineFactory): |
5
4ec829dc7d1d
Renamed ChatInstructions to 'to_send()', 'to_receive()' for readability.
Atul Varma <varmaa@toolness.com>
parents:
4
diff
changeset
|
117 coroutine = coroutineFactory(addr) |
11
8574ff006a28
instructions are now objects.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
118 _AsyncChatCoroutineDispatcher.__init__(self, coroutine) |
2
f6f9cc0385be
Clients can now be coroutines too.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
119 self.create_socket(socket.AF_INET, socket.SOCK_STREAM) |
f6f9cc0385be
Clients can now be coroutines too.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
120 self.connect(addr) |
f6f9cc0385be
Clients can now be coroutines too.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
121 |
6
fdcb6f3e1a0f
Separated out example code into a separate file.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
122 # Instructions that coroutines yield. |
fdcb6f3e1a0f
Separated out example code into a separate file.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
123 |
11
8574ff006a28
instructions are now objects.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
124 class until_received(object): |
8574ff006a28
instructions are now objects.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
125 def __init__(self, terminator = None, bytes = None): |
8574ff006a28
instructions are now objects.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
126 if terminator: |
8574ff006a28
instructions are now objects.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
127 self._terminator = terminator |
8574ff006a28
instructions are now objects.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
128 elif bytes: |
8574ff006a28
instructions are now objects.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
129 self._terminator = bytes |
8574ff006a28
instructions are now objects.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
130 else: |
8574ff006a28
instructions are now objects.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
131 raise ValueError() |
1
f1159b9ec823
Initial implementation of coroutine-based server.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
132 |
11
8574ff006a28
instructions are now objects.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
133 def execute(self, dispatcher): |
8574ff006a28
instructions are now objects.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
134 dispatcher.set_terminator(self._terminator) |
8574ff006a28
instructions are now objects.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
135 |
8574ff006a28
instructions are now objects.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
136 class until_sent(object): |
8574ff006a28
instructions are now objects.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
137 def __init__(self, content): |
8574ff006a28
instructions are now objects.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
138 if not content: |
8574ff006a28
instructions are now objects.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
139 raise ValueError(content) |
8574ff006a28
instructions are now objects.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
140 self.content = content |
8574ff006a28
instructions are now objects.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
141 |
8574ff006a28
instructions are now objects.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
142 def execute(self, dispatcher): |
8574ff006a28
instructions are now objects.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
143 dispatcher.push(self.content) |
22
425776983cf2
Removed the weakref mechanism from channels.py and instead turned until_message_received() into a generator rather than an object. Added a return_value() instruction to cosocket.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
144 |
425776983cf2
Removed the weakref mechanism from channels.py and instead turned until_message_received() into a generator rather than an object. Added a return_value() instruction to cosocket.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
145 class return_value(object): |
425776983cf2
Removed the weakref mechanism from channels.py and instead turned until_message_received() into a generator rather than an object. Added a return_value() instruction to cosocket.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
146 def __init__(self, value): |
425776983cf2
Removed the weakref mechanism from channels.py and instead turned until_message_received() into a generator rather than an object. Added a return_value() instruction to cosocket.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
147 self.value = value |
425776983cf2
Removed the weakref mechanism from channels.py and instead turned until_message_received() into a generator rather than an object. Added a return_value() instruction to cosocket.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
148 |
425776983cf2
Removed the weakref mechanism from channels.py and instead turned until_message_received() into a generator rather than an object. Added a return_value() instruction to cosocket.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
149 def execute(self, dispatcher): |
425776983cf2
Removed the weakref mechanism from channels.py and instead turned until_message_received() into a generator rather than an object. Added a return_value() instruction to cosocket.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
150 dispatcher.close_coroutine_and_return_to_caller(self.value) |