Skip to content

Commit

Permalink
Py3k Compatibility (v3.2.4)
Browse files Browse the repository at this point in the history
* Fixed common.FirstByteProtocol().recv() for Python 3
* Added some logging to common.FirstByteProtocol()
* Added ability to change the chunk size to common.FirstByteProtocol()
  • Loading branch information
pydsigner committed Nov 8, 2012
1 parent 5f2325e commit bfe8ccd
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion taskit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""

__version__ = '3.2.3'
__version__ = '3.2.4'
3 changes: 1 addition & 2 deletions taskit/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,11 @@ def __init__(self, tasks, host='127.0.0.1', port=DEFAULT_PORT,
use to determine the responsiveness: it is used for socket
timeouts and while:sleep() wait loops.
"""
FirstByteProtocol.__init__(self)
FirstByteProtocol.__init__(self, logger)

self.tasks = tasks
self.host = host
self.port = port
self.log = logger
self.codec = codec
self.task_count = 0
# Is this necessary to avoid problems with the task counter getting
Expand Down
26 changes: 18 additions & 8 deletions taskit/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import json
import pickle

from .log import null_logger, ERROR


__all__ = ['DEFAULT_PORT', 'STOP', 'KILL', 'STATUS', 'bytes', 'basestring',
'FirstByteCorruptionError', 'FirstByteProtocol', 'JSONCodec',
Expand Down Expand Up @@ -40,31 +42,39 @@ class FirstByteProtocol(object):

first = 4

def __init__(self, send_size=2048):
def __init__(self, logger=null_logger, send_size=2048):
"""
send_size -- The maximum length of the message pieces created. Will not
be exceeded, but will often not be reached. This value
should not exceed 8192 (the largest power of 2 with a
length of 4)
"""
self.set_size(send_size)
self.log = logger

def set_size(self, send_size):
self.send_size = send_size
self.data_size = send_size - 1
self.first_msg = bytes(str(self.send_size).zfill(self.first), 'utf-8')

def recv(self, sock):
size = int(sock.recv(self.first))

data = b''
bit = b'1'
while bit == b'1':
raw = sock.recv(size)
data = ''
bit = '1'
while bit == '1':
raw = sock.recv(size).decode()

bit = raw[0]
data += raw[1:]
if bit not in (b'0', b'1'):
if bit not in ('0', '1'):
self.log(ERROR, 'First char %r not one of "0" or "1"!' % bit)
raise FirstByteCorruptionError(
'Protocol corruption detected! Check that the other side is '
'not sending bigger chunks than this side is receiving.')
return data.decode()

data += raw[1:]

return data

def send(self, sock, data):
sock.send(self.first_msg)
Expand Down
3 changes: 1 addition & 2 deletions taskit/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,8 @@ def __init__(self, backends=(), default_port=DEFAULT_PORT,
logger -- A logger supporting the taskit.log interface.
codec -- A codec to be used in converting messages into strings.
"""
FirstByteProtocol.__init__(self)
FirstByteProtocol.__init__(self, logger)

self.log = logger
self.default_port = default_port
self.backends = {}
self.task_counter = {}
Expand Down

0 comments on commit bfe8ccd

Please sign in to comment.