Skip to content

Instantly share code, notes, and snippets.

@methane
Created October 15, 2012 04:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save methane/3890799 to your computer and use it in GitHub Desktop.
Save methane/3890799 to your computer and use it in GitHub Desktop.
redisをmeinheldから使ってみたら動かなかった時のコード
from meinheld import server, patch
patch.patch_socket()
import greenlet
main_greenlet = greenlet.getcurrent()
import redis
def sleep(sec):
server.schedule_call(greenlet.getcurrent().switch)
main_greenlet.switch()
def writer():
con = redis.Redis()
while True:
print "sending"
con.publish('test_pubsub', 'hello')
print "sent"
sleep(1)
def reader():
con = redis.Redis()
pubsub = con.pubsub()
pubsub.subscribe('test_pubsub')
for msg in pubsub.listen():
print msg
server.spawn(writer)
server.spawn(reader)
def dummy_app(env, start):
start("200 OK", [('Content-Type', 'text/plain')])
return ['Hello']
server.listen("127.0.0.1:8081")
server.run(dummy_app)
redis_sample.py(17): con.publish('test_pubsub', 'hello')
--- modulename: client, funcname: publish
client.py(1287): return self.execute_command('PUBLISH', channel, message)
--- modulename: client, funcname: execute_command
client.py(355): pool = self.connection_pool
client.py(356): command_name = args[0]
client.py(357): connection = pool.get_connection(command_name, **options)
--- modulename: connection, funcname: get_connection
connection.py(391): self._checkpid()
--- modulename: connection, funcname: _checkpid
connection.py(384): if self.pid != os.getpid():
connection.py(392): try:
connection.py(393): connection = self._available_connections.pop()
connection.py(394): except IndexError:
connection.py(395): connection = self.make_connection()
--- modulename: connection, funcname: make_connection
connection.py(401): if self._created_connections >= self.max_connections:
connection.py(403): self._created_connections += 1
connection.py(404): return self.connection_class(**self.connection_kwargs)
--- modulename: connection, funcname: __init__
connection.py(203): self.pid = os.getpid()
connection.py(204): self.host = host
connection.py(205): self.port = port
connection.py(206): self.db = db
connection.py(207): self.password = password
connection.py(208): self.socket_timeout = socket_timeout
connection.py(209): self.encoding = encoding
connection.py(210): self.encoding_errors = encoding_errors
connection.py(211): self.decode_responses = decode_responses
connection.py(212): self._sock = None
connection.py(213): self._parser = parser_class()
--- modulename: connection, funcname: __init__
connection.py(41): self._fp = None
connection.py(396): self._in_use_connections.add(connection)
connection.py(397): return connection
client.py(358): try:
client.py(359): connection.send_command(*args)
--- modulename: connection, funcname: send_command
connection.py(299): self.send_packed_command(self.pack_command(*args))
--- modulename: connection, funcname: pack_command
connection.py(326): output = SYM_STAR + b(str(len(args))) + SYM_CRLF
--- modulename: _compat, funcname: <lambda>
_compat.py(20): b = lambda x: x
connection.py(327): for enc_value in imap(self.encode, args):
--- modulename: connection, funcname: encode
connection.py(314): if isinstance(value, bytes):
connection.py(315): return value
connection.py(328): output += SYM_DOLLAR
connection.py(329): output += b(str(len(enc_value)))
--- modulename: _compat, funcname: <lambda>
_compat.py(20): b = lambda x: x
connection.py(330): output += SYM_CRLF
connection.py(331): output += enc_value
connection.py(332): output += SYM_CRLF
connection.py(327): for enc_value in imap(self.encode, args):
--- modulename: connection, funcname: encode
connection.py(314): if isinstance(value, bytes):
connection.py(315): return value
connection.py(328): output += SYM_DOLLAR
connection.py(329): output += b(str(len(enc_value)))
--- modulename: _compat, funcname: <lambda>
_compat.py(20): b = lambda x: x
connection.py(330): output += SYM_CRLF
connection.py(331): output += enc_value
connection.py(332): output += SYM_CRLF
connection.py(327): for enc_value in imap(self.encode, args):
--- modulename: connection, funcname: encode
connection.py(314): if isinstance(value, bytes):
connection.py(315): return value
connection.py(328): output += SYM_DOLLAR
connection.py(329): output += b(str(len(enc_value)))
--- modulename: _compat, funcname: <lambda>
_compat.py(20): b = lambda x: x
connection.py(330): output += SYM_CRLF
connection.py(331): output += enc_value
connection.py(332): output += SYM_CRLF
connection.py(327): for enc_value in imap(self.encode, args):
connection.py(333): return output
--- modulename: connection, funcname: send_packed_command
connection.py(280): if not self._sock:
connection.py(281): self.connect()
--- modulename: connection, funcname: connect
connection.py(223): if self._sock:
connection.py(225): try:
connection.py(226): sock = self._connect()
--- modulename: connection, funcname: _connect
connection.py(236): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
--- modulename: msocket, funcname: __init__
msocket.py(499): if _sock is None:
msocket.py(500): self._sock = _realsocket(family, type, proto)
msocket.py(501): self.timeout = _socket.getdefaulttimeout()
msocket.py(511): self._sock.setblocking(0)
msocket.py(512): self.setsockopt(IPPROTO_TCP, TCP_NODELAY, 1)
--- modulename: msocket, funcname: setsockopt
<string>(1): connection.py(237): sock.settimeout(self.socket_timeout)
--- modulename: msocket, funcname: internal_settimeout
msocket.py(393): if howlong is not None:
msocket.py(401): s.timeout = howlong
connection.py(238): sock.connect((self.host, self.port))
--- modulename: msocket, funcname: internal_connect
msocket.py(206): if s.timeout == 0.0:
msocket.py(208): sock = s._sock
msocket.py(209): if s.timeout is None:
msocket.py(210): while True:
msocket.py(211): err = sock.getsockopt(SOL_SOCKET, SO_ERROR)
msocket.py(212): if err:
msocket.py(214): result = sock.connect_ex(address)
msocket.py(215): if not result or result == EISCONN:
msocket.py(217): elif (result in (EWOULDBLOCK, EINPROGRESS, EALREADY)) or (result == EINVAL and is_windows):
msocket.py(218): wait_readwrite(sock.fileno())
--- modulename: msocket, funcname: wait_readwrite
msocket.py(158): if not timeout:
msocket.py(159): timeout = 0
msocket.py(160): server.trampoline(fileno, read=True, write=True, timeout=int(timeout))
--- modulename: redis_sample, funcname: reader
redis_sample.py(22): con = redis.Redis()
--- modulename: client, funcname: __init__
client.py(270): if not connection_pool:
client.py(271): kwargs = {
client.py(272): 'db': db,
client.py(273): 'password': password,
client.py(274): 'socket_timeout': socket_timeout,
client.py(275): 'encoding': charset,
client.py(276): 'encoding_errors': errors,
client.py(277): 'decode_responses': decode_responses,
client.py(280): if unix_socket_path:
client.py(286): kwargs.update({
client.py(287): 'host': host,
client.py(288): 'port': port
client.py(290): connection_pool = ConnectionPool(**kwargs)
--- modulename: connection, funcname: __init__
connection.py(375): self.pid = os.getpid()
connection.py(376): self.connection_class = connection_class
connection.py(377): self.connection_kwargs = connection_kwargs
connection.py(378): self.max_connections = max_connections or 2 ** 31
connection.py(379): self._created_connections = 0
connection.py(380): self._available_connections = []
connection.py(381): self._in_use_connections = set()
client.py(291): self.connection_pool = connection_pool
client.py(293): self.response_callbacks = self.__class__.RESPONSE_CALLBACKS.copy()
redis_sample.py(23): pubsub = con.pubsub()
--- modulename: client, funcname: pubsub
client.py(350): return PubSub(self.connection_pool, shard_hint)
--- modulename: client, funcname: __init__
client.py(1438): self.connection_pool = connection_pool
client.py(1439): self.shard_hint = shard_hint
client.py(1440): self.connection = None
client.py(1441): self.channels = set()
client.py(1442): self.patterns = set()
client.py(1443): self.subscription_count = 0
client.py(1444): self.subscribe_commands = set(
client.py(1445): ('subscribe', 'psubscribe', 'unsubscribe', 'punsubscribe')
redis_sample.py(24): pubsub.subscribe('test_pubsub')
--- modulename: client, funcname: subscribe
client.py(1531): if isinstance(channels, basestring):
client.py(1532): channels = [channels]
client.py(1533): for channel in channels:
client.py(1534): self.channels.add(channel)
client.py(1533): for channel in channels:
client.py(1535): return self.execute_command('SUBSCRIBE', *channels)
--- modulename: client, funcname: execute_command
client.py(1475): if self.connection is None:
client.py(1476): self.connection = self.connection_pool.get_connection(
client.py(1477): 'pubsub',
client.py(1478): self.shard_hint
--- modulename: connection, funcname: get_connection
connection.py(391): self._checkpid()
--- modulename: connection, funcname: _checkpid
connection.py(384): if self.pid != os.getpid():
connection.py(392): try:
connection.py(393): connection = self._available_connections.pop()
connection.py(394): except IndexError:
connection.py(395): connection = self.make_connection()
--- modulename: connection, funcname: make_connection
connection.py(401): if self._created_connections >= self.max_connections:
connection.py(403): self._created_connections += 1
connection.py(404): return self.connection_class(**self.connection_kwargs)
--- modulename: connection, funcname: __init__
connection.py(203): self.pid = os.getpid()
connection.py(204): self.host = host
connection.py(205): self.port = port
connection.py(206): self.db = db
connection.py(207): self.password = password
connection.py(208): self.socket_timeout = socket_timeout
connection.py(209): self.encoding = encoding
connection.py(210): self.encoding_errors = encoding_errors
connection.py(211): self.decode_responses = decode_responses
connection.py(212): self._sock = None
connection.py(213): self._parser = parser_class()
--- modulename: connection, funcname: __init__
connection.py(41): self._fp = None
connection.py(396): self._in_use_connections.add(connection)
connection.py(397): return connection
client.py(1480): connection = self.connection
client.py(1481): try:
client.py(1482): connection.send_command(*args)
--- modulename: connection, funcname: send_command
connection.py(299): self.send_packed_command(self.pack_command(*args))
--- modulename: connection, funcname: pack_command
connection.py(326): output = SYM_STAR + b(str(len(args))) + SYM_CRLF
--- modulename: _compat, funcname: <lambda>
_compat.py(20): b = lambda x: x
connection.py(327): for enc_value in imap(self.encode, args):
--- modulename: connection, funcname: encode
connection.py(314): if isinstance(value, bytes):
connection.py(315): return value
connection.py(328): output += SYM_DOLLAR
connection.py(329): output += b(str(len(enc_value)))
--- modulename: _compat, funcname: <lambda>
_compat.py(20): b = lambda x: x
connection.py(330): output += SYM_CRLF
connection.py(331): output += enc_value
connection.py(332): output += SYM_CRLF
connection.py(327): for enc_value in imap(self.encode, args):
--- modulename: connection, funcname: encode
connection.py(314): if isinstance(value, bytes):
connection.py(315): return value
connection.py(328): output += SYM_DOLLAR
connection.py(329): output += b(str(len(enc_value)))
--- modulename: _compat, funcname: <lambda>
_compat.py(20): b = lambda x: x
connection.py(330): output += SYM_CRLF
connection.py(331): output += enc_value
connection.py(332): output += SYM_CRLF
connection.py(327): for enc_value in imap(self.encode, args):
connection.py(333): return output
--- modulename: connection, funcname: send_packed_command
connection.py(280): if not self._sock:
connection.py(281): self.connect()
--- modulename: connection, funcname: connect
connection.py(223): if self._sock:
connection.py(225): try:
connection.py(226): sock = self._connect()
--- modulename: connection, funcname: _connect
connection.py(236): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
--- modulename: msocket, funcname: __init__
msocket.py(499): if _sock is None:
msocket.py(500): self._sock = _realsocket(family, type, proto)
msocket.py(501): self.timeout = _socket.getdefaulttimeout()
msocket.py(511): self._sock.setblocking(0)
msocket.py(512): self.setsockopt(IPPROTO_TCP, TCP_NODELAY, 1)
--- modulename: msocket, funcname: setsockopt
<string>(1): connection.py(237): sock.settimeout(self.socket_timeout)
--- modulename: msocket, funcname: internal_settimeout
msocket.py(393): if howlong is not None:
msocket.py(401): s.timeout = howlong
connection.py(238): sock.connect((self.host, self.port))
--- modulename: msocket, funcname: internal_connect
msocket.py(206): if s.timeout == 0.0:
msocket.py(208): sock = s._sock
msocket.py(209): if s.timeout is None:
msocket.py(210): while True:
msocket.py(211): err = sock.getsockopt(SOL_SOCKET, SO_ERROR)
msocket.py(212): if err:
msocket.py(214): result = sock.connect_ex(address)
msocket.py(215): if not result or result == EISCONN:
msocket.py(217): elif (result in (EWOULDBLOCK, EINPROGRESS, EALREADY)) or (result == EINVAL and is_windows):
msocket.py(218): wait_readwrite(sock.fileno())
--- modulename: msocket, funcname: wait_readwrite
msocket.py(158): if not timeout:
msocket.py(159): timeout = 0
msocket.py(160): server.trampoline(fileno, read=True, write=True, timeout=int(timeout))
^C^C^C --- modulename: trace, funcname: _unsettrace
trace.py(80): sys.settrace(None)
Traceback (most recent call last):
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 162, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/trace.py", line 819, in <module>
main()
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/trace.py", line 807, in main
t.runctx(code, globs, globs)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/trace.py", line 513, in runctx
exec cmd in globals, locals
File "redis_sample.py", line 36, in <module>
server.run(dummy_app)
KeyboardInterrupt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment