Friday 15 March 2013

sockets - Runing class method multiple times parallel in Python -



sockets - Runing class method multiple times parallel in Python -

i have implemented python socket server. sends image info multiple cameras client. request handler class looks like:

class requesthandler(socketserver.baserequesthandler): def handle(self): while true: info = self.request.recv(1024) if data.endswith('0000000050'): # client requests info camera_id, camera_path in _video_devices.iteritems(): message = self.create_image_transfer_message(camera_id, camera_path) self.request.sendto(message, self.client_address) def create_image_transfer_message(self, camera_id, camera_path): # somecode ...

i forced stick socket server because of client. works problem works sequentially, there big delays between photographic camera images beingness uploaded. create transfer messages in parallel little delay between calls.

i tried utilize pool class multiprocessing:

import multiprocessing class requesthandler(socketserver.baserequesthandler): def handle(self): ... pool = multiprocessing.pool(processes=4) messages = [pool.apply(self.create_image_transfer_message, args=(camera_id, camera_path)) camid, campath in _video_devices.iteritems()]

but throws:

picklingerror: can't pickle <type 'function'>: attribute lookup __builtin__.function failed

i want know if there way create transfer messages in parallel defined delay between calls?

edit:

i create response messages using info multiple cameras. problem is, if run image grabbing routines close each other image artifacts, because usb bus overloaded. figured out, calling image grabbing sequentially 0.2 sec delay solve problem. cameras not sending info whole time image grabbing function running, delayed parallel cal result in images little delay between them.

i think you're on right path already, no need throw away work.

here's reply how utilize class method multiprocessing found via google after searching "multiprocessing class method"

from multiprocessing import pool import time pool = pool(processes=2) def unwrap_self_f(arg, **kwarg): homecoming c.create_image_transfer_message(*arg, **kwarg) class requesthandler(socketserver.baserequesthandler): @classmethod def create_image_transfer_message(cls, camera_id, camera_path): # logic goes here def handle(self): while true: info = self.request.recv(1024) if not data.endswith('0000000050'): # client requests info go on pool.map(unwrap_self_f, ( (camera_id, camera_path) camera_id, camera_path in _video_devices.iteritems() ) )

note, if want homecoming values workers you'll need explore using shared resource see reply here - how can recover homecoming value of function passed multiprocessing.process?

python sockets multiprocessing socketserver

No comments:

Post a Comment