[python] multiprocess manager

Tomas Pelka tompelka na gmail.com
Pondělí Červenec 26 21:52:45 CEST 2010


Zdravim vsechny,

mam potize s exportovanim objektu pres manager z modulu multiprocess, 
priklad:

Worker.py:
###################################
from multiprocessing import Process
from multiprocessing.managers import BaseManager
import pcapy
from impacket.ImpactDecoder import EthDecoder

__all__ = ['Worker']

class Worker(Process):
         '''
         Class for sniffing packets, runnig as root
         '''

         public = ['go', 'terminate']

         def __init__(self):
                 super(Worker, self).__init__()
                 self.iface = ''
                 self.expr = ''
                 self.pcap = ''
                 # define packet decoder
                 self.decoder = EthDecoder()
                 # key for queue daemon, remotely on localhost:5000
                 self._keyQ = '10b222970537b97919db36ec757370d2'
                 class QueueManager(BaseManager): pass
                 QueueManager.register('get_dataQueue')
                 self._m = QueueManager(address=('127.0.0.1', 5000), 
authkey=self._keyQ)
                 self._m.connect()
                 self.dataQueue = self._m.get_dataQueue()
         def go(self, iface, expr):
                 '''
                 start sniffer
                 '''
                 print "Starting sniffer"
                 self.iface = iface
                 self.expr = expr
                 super(Worker, self).start()
         def terminate(self):
                 '''
                 terminate sniffer
                 '''
                 super(Worker, self).terminate()
         def run(self):
                 print "sniffing ..."
                 print self.iface
                 print self.expr
                 self.pcap = pcapy.open_live(self.iface, 1500, 1, 0)
                 self.pcap.setfilter(self.expr)
                 self.pcap.loop(0, self.__packetHandler)
                 print "... done"
         def __packetHandler(self, hdr, data):
                 '''
                 handles packets and put them in to the queue
                 '''
                 print "Handling packets"
                 #print data
                 print "Queue size: %i" % self.dataQueue.qsize()
                 print self.decoder.decode(data)
                 self.dataQueue.put(data)

Export objektu (Worker):
###################################
from Worker import Worker

class SniffManager(BaseManager): pass
SniffManager.register('Worker', callable=Worker)
Sm = SniffManager(address=('127.0.0.1', 5001), 
authkey='f1f16683f3e0208131b46d37a79c8921')
Ss = Sm.get_server()
Ss.serve_forever()


Volani metod objektu, vzdalene:
###################################
# get remote object
class WorkerManager(BaseManager): pass
WorkerManager.register('Worker')
w = WorkerManager(address=('127.0.0.1', 5001), 
authkey='f1f16683f3e0208131b46d37a79c8921')
w.connect()
worker = w.Worker() #TOHLE FUNGUJE JAK MA

ale

worker.terminate()

File "/home/tom/web2py/applications/init/controllers/sniffer.py", line 
143, in index
     worker.terminate()
File "<string>", line 2, in terminate
File "/usr/lib/python2.6/multiprocessing/managers.py", line 740, in 
_callmethod
     raise convert_to_error(kind, result)
AttributeError: 'NoneType' object has no attribute 'terminate'

Zarazi me ze go() funguje a terminate() nikoliv. Lokalne funfuje vse tak 
jak ma.

Diky za rady.

-- 
Tomas Pelka

------------- dal?í ?ást ---------------
HTML p?íloha byla odstran?na...
URL: <http://www.py.cz/pipermail/python/attachments/20100726/2e39bfb3/attachment.html>


Další informace o konferenci Python