python - Why wont this pickle? -
i trying run process multiprocessing, when starting thread pickler falls on , can't work out stopping pickling. i've tried commenting out socket code , message obj code, still not working - doing wrong?
class transmitthread(process): def __init__(self, send_queue, reply_queue, control_pipe, recv_timeout=2, buffer_size=4096): """ init function called when thread created. function calls process class init function, , stores class vars. """ # call process class init process.__init__(self) # store class vars self.send_queue = send_queue self.reply_queue = reply_queue self.control_pipe = control_pipe self._recv_timeout = recv_timeout self._buffer_size = buffer_size def run(self): """ main function called when thread started. function loops forever, waiting send message in queue, , processes message send , fetches response. thread loops forever until it's terminated or kill thread command passed through control pipe. """ # start our forever running loop while true: # check if there in pipe if self.control_pipe.poll(): # check if received kill thread command if self.control_pipe.recv() == kill_thread_command: # kill while loop , end thread break # check if there in message queue if not self.send_queue.empty(): # fetch message queue send, , unpickle message_obj, message_pickle = self.send_queue.get() # open socket , set timeout sock = socket.socket(socket.af_inet, socket.sock_dgram) sock.settimeout(self._recv_timeout) # connect socket recipient sock.connect( message_obj.recipient_address ) # push pickled message down socket sock.sendall(message_pickle) # check if message send request (should response) if str(message_obj.message_type) == str(message.request): print "fetching reply" # lets fetch response, , push pickled message onto queue self.reply_queue.put( sock.recv(self._buffer_size) ) print "got reply" # done, close socket sock.close() # add small delay stop thread consuming cpu time sleep(0.1)
the error message is:
file "c:/users/oliver/onedrive/git/pyke/pyke.py", line 127, in __init__ self.thread.start() file "c:\python27\lib\multiprocessing\process.py", line 130, in start self._popen = popen(self) file "c:\python27\lib\multiprocessing\forking.py", line 277, in __init__ dump(process_obj, to_child, highest_protocol) file "c:\python27\lib\multiprocessing\forking.py", line 199, in dump forkingpickler(file, protocol).dump(obj) file "c:\python27\lib\pickle.py", line 224, in dump self.save(obj) file "c:\python27\lib\pickle.py", line 331, in save self.save_reduce(obj=obj, *rv) file "c:\python27\lib\pickle.py", line 419, in save_reduce save(state) file "c:\python27\lib\pickle.py", line 286, in save f(self, obj) # call unbound method explicit self file "c:\python27\lib\pickle.py", line 649, in save_dict self._batch_setitems(obj.iteritems()) file "c:\python27\lib\pickle.py", line 681, in _batch_setitems save(v) file "c:\python27\lib\pickle.py", line 286, in save f(self, obj) # call unbound method explicit self file "c:\python27\lib\pickle.py", line 725, in save_inst save(stuff) file "c:\python27\lib\pickle.py", line 286, in save f(self, obj) # call unbound method explicit self file "c:\python27\lib\pickle.py", line 649, in save_dict self._batch_setitems(obj.iteritems()) file "c:\python27\lib\pickle.py", line 681, in _batch_setitems save(v) file "c:\python27\lib\pickle.py", line 331, in save self.save_reduce(obj=obj, *rv) file "c:\python27\lib\pickle.py", line 419, in save_reduce save(state) file "c:\python27\lib\pickle.py", line 286, in save f(self, obj) # call unbound method explicit self file "c:\python27\lib\pickle.py", line 649, in save_dict self._batch_setitems(obj.iteritems()) file "c:\python27\lib\pickle.py", line 681, in _batch_setitems save(v) file "c:\python27\lib\pickle.py", line 331, in save self.save_reduce(obj=obj, *rv) file "c:\python27\lib\pickle.py", line 396, in save_reduce save(cls) file "c:\python27\lib\pickle.py", line 286, in save f(self, obj) # call unbound method explicit self file "c:\python27\lib\pickle.py", line 748, in save_global (obj, module, name)) pickle.picklingerror: can't pickle <type 'thread.lock'>: it's not found thread.lock traceback (most recent call last): file "<string>", line 1, in <module> file "c:\python27\lib\multiprocessing\forking.py", line 381, in main self = load(from_parent) file "c:\python27\lib\pickle.py", line 1378, in load return unpickler(file).load() file "c:\python27\lib\pickle.py", line 858, in load dispatch[key](self) file "c:\python27\lib\pickle.py", line 880, in load_eof raise eoferror eoferror
the pickler error comes multiprocessing.process
trying internally pickle subprocess. i'm pretty sure 1 of instance variables doesn't pickle child process. 1 not clear question
# store class vars self.send_queue = send_queue self.reply_queue = reply_queue self.control_pipe = control_pipe self._recv_timeout = recv_timeout self._buffer_size = buffer_size
[edit after comments op]:
the problem send_queue
, reply_queue
queue.queue
s rather multiprocessing.queue
. when forking child worker, process
tries serialize , instance variables on child. queue.queue
s local objects not serializable, hence error.
also related question fact that, multiprocessing.queue
re-uses exceptions queue
without re-exporting these. is documented, albeit hidden beneath clutter:
note:
multiprocessing
uses usualqueue.empty
,queue.full
exceptions signal timeout. not available inmultiprocessing
namespace need import themqueue
.
Comments
Post a Comment