Skip to content

Commit

Permalink
Merge branch 'peterscott-timeout'
Browse files Browse the repository at this point in the history
  • Loading branch information
earl committed Jan 28, 2011
2 parents 6594abc + 8f20815 commit cc22cef
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 4 additions & 1 deletion TUTORIAL.mkd
Expand Up @@ -23,7 +23,10 @@ To use beanstalkc we have to import the library and set up a connection to an
>>> beanstalk = beanstalkc.Connection(host='localhost', port=14711)

If we leave out the `host` and/or `port` parameters, `'localhost'` and `11300`
would be used as defaults, respectively.
would be used as defaults, respectively. There is also a `connect_timeout`
parameter which determines how long, in seconds, the socket will wait for the
server to respond to its initial connection attempt. If it is `None`, then
there will be no timeout; it defaults to 1.


Basic Operation
Expand Down
6 changes: 5 additions & 1 deletion beanstalkc.py
Expand Up @@ -44,13 +44,15 @@ def wrap(fn, *args, **kwargs):


class Connection(object):
def __init__(self, host=DEFAULT_HOST, port=DEFAULT_PORT, parse_yaml=True):
def __init__(self, host=DEFAULT_HOST, port=DEFAULT_PORT, parse_yaml=True,
connect_timeout=socket.getdefaulttimeout()):
if parse_yaml is True:
try:
parse_yaml = __import__('yaml').load
except ImportError:
logging.error('Failed to load PyYAML, will not parse YAML')
parse_yaml = False
self._connect_timeout= connect_timeout
self._parse_yaml = parse_yaml or (lambda x: x)
self.host = host
self.port = port
Expand All @@ -59,7 +61,9 @@ def __init__(self, host=DEFAULT_HOST, port=DEFAULT_PORT, parse_yaml=True):
def connect(self):
"""Connect to beanstalkd server."""
self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._socket.settimeout(self._connect_timeout)
SocketError.wrap(self._socket.connect, (self.host, self.port))
self._socket.settimeout(None)
self._socket_file = self._socket.makefile('rb')

def close(self):
Expand Down

0 comments on commit cc22cef

Please sign in to comment.