-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemcached_client3.py
More file actions
34 lines (27 loc) · 1.01 KB
/
memcached_client3.py
File metadata and controls
34 lines (27 loc) · 1.01 KB
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
from twisted.protocols import basic
from twisted.internet import defer
class MemcachedSetProtocol(basic.LineReceiver):
def set(self, key, value, flags, timeout=0):
self.deferred = defer.Deferred()
length = len(value)
self.sendLine('set %s %d %d %d' % (key, flags, timeout, length))
self.transport.write(value)
self.sendLine('')
return self.deferred
def lineReceived(self, line):
if line == 'STORED':
self.deferred.callback(None)
if __name__ == '__main__':
from twisted.internet import reactor, endpoints, protocol
endpoint = endpoints.TCP4ClientEndpoint(reactor, '127.0.0.1', 11211)
factory = protocol.Factory()
factory.protocol = MemcachedSetProtocol
d = endpoint.connect(factory)
def got_protocol(p):
deferredValue = p.set('a', '123', 12, 0)
def set_v(_):
print 'STORED'
reactor.stop()
deferredValue.addCallback(set_v)
d.addCallback(got_protocol)
reactor.run()