Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AF_UNIX support for beanstalkc #29

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions TUTORIAL.mkd
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ server to respond to its initial connection attempt. If it is `None`, then
there will be no timeout; it defaults to the result of your system's
`socket.getdefaulttimeout()`.

Starting with Beanstalkd 1.9 it is possible to connect through a Unix socket.
The `path` parameter allows to enable this behaviour. Therefore, if you started
the daemon with `beanstalkd -L /tmp/beans`, you can connect to it like the
following:

>>> beanstalk = beanstalkc.Connection(path='/tmp/beans') #doctest:+SKIP

Note that when you use the `path` parameter, both `host` and `port` are ignored.

Basic Operation
---------------
Expand Down
15 changes: 11 additions & 4 deletions beanstalkc.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ def wrap(wrapped_function, *args, **kwargs):


class Connection(object):
def __init__(self, host=DEFAULT_HOST, port=DEFAULT_PORT, parse_yaml=True,
connect_timeout=socket.getdefaulttimeout()):
def __init__(self, host=DEFAULT_HOST, port=DEFAULT_PORT, path=None,
parse_yaml=True, connect_timeout=socket.getdefaulttimeout()):
if parse_yaml is True:
try:
parse_yaml = __import__('yaml').load
Expand All @@ -56,13 +56,20 @@ def __init__(self, host=DEFAULT_HOST, port=DEFAULT_PORT, parse_yaml=True,
self._parse_yaml = parse_yaml or (lambda x: x)
self.host = host
self.port = port
self.path = path
self.connect()

def connect(self):
"""Connect to beanstalkd server."""
self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if self.path:
family = socket.AF_UNIX
target = self.path
else:
family = socket.AF_INET
target = (self.host, self.port)
self._socket = socket.socket(family, socket.SOCK_STREAM)
self._socket.settimeout(self._connect_timeout)
SocketError.wrap(self._socket.connect, (self.host, self.port))
SocketError.wrap(self._socket.connect, target)
self._socket.settimeout(None)
self._socket_file = self._socket.makefile('rb')

Expand Down