многопроцессорная обработка и базовый HTTPServer

#python #multiprocessing

#python #многопроцессорная обработка

Вопрос:

Я пытаюсь разработать HTTPServer, используя базовый HTTPServer и многопроцессорную обработку Python. Я выполняю многопроцессорную обработку, чтобы одновременно выполнять несколько скриптов Python.

Пожалуйста, обратите внимание, что Script1 — это просто простой скрипт, имеющий время печати цикла while.asctime() в течение 5 секунд

Полный код здесь:

 """ HTTP Server to run multiple python scripts via multiprocessing?
"""

from threading import Thread
from subprocess import PIPE, Popen
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
import time
import multiprocessing

def simple_script(request_handler):
    print 'simple_script started', time.asctime()
    print 'request_handler', request_handler
    s = Popen('C:/Python27/python C:/Script1.py 5', shell=True,
              stdout=PIPE, stderr=PIPE)
    out, err = s.communicate()
    print out, err
    request_handler.wfile.write(out)
    request_handler.wfile.write(err)
    request_handler.wfile.write('n')
    print 'simple_script ended', time.asctime()

class Handler(BaseHTTPRequestHandler):
    def handle(self):
        """ Overriding this method of SocketServer.BaseRequestHandler
        in order to implement my own service

        """

        print 'Reading', self.rfile.readline()
        print 'Handling request started', time.asctime()
        t = multiprocessing.Process(target=simple_script, args=(self,))
        t.start()
        time.sleep(2)
        print 'Handling request ended', time.asctime()

    def finish(self):
        pass

class MyHTTPServer(HTTPServer):
    def process_request(self, request, client_address):
        """Call finish_request.

        Overridden by ForkingMixIn and ThreadingMixIn.

        """
        self.finish_request(request, client_address)
        #self.shutdown_request(request)

if __name__ == '__main__':
    server = MyHTTPServer(('', 8080), Handler)
    print 'Starting server, use <Ctrl-C> to stop'
    server.serve_forever()
  

Я получаю эту ошибку:

 <code>
Starting server, use <Ctrl-C> to stop
Reading GET / HTTP/1.1

Handling request started Mon Oct 10 14:44:24 2011
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 65046)
Traceback (most recent call last):
  File "C:Python27libSocketServer.py", line 284, in _handle_request_noblock
    self.process_request(request, client_address)
  File "C:BarikUCS automationeclipse_workspaceUCSPython_HTTPsrcthreading_with_httpproblem_multiprocessing.py", line 46, in process_request
----------------------------------------
    self.finish_request(request, client_address)
  File "C:Python27libSocketServer.py", line 323, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "C:Python27libSocketServer.py", line 639, in __init__
    self.handle()
  File "C:BarikUCS automationeclipse_workspaceUCSPython_HTTPsrcthreading_with_httpproblem_multiprocessing.py", line 32, in handle
    t.start()
  File "C:Python27libmultiprocessingprocess.py", line 104, in start
    self._popen = Popen(self)
  File "C:Python27libmultiprocessingforking.py", line 244, in __init__
    dump(process_obj, to_child, HIGHEST_PROTOCOL)
  File "C:Python27libmultiprocessingforking.py", line 167, in dump
    ForkingPickler(file, protocol).dump(obj)
  File "C:Python27libpickle.py", line 224, in dump
    self.save(obj)
  File "C:Python27libpickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)
  File "C:Python27libpickle.py", line 419, in save_reduce
    save(state)
  File "C:Python27libpickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:Python27libpickle.py", line 649, in save_dict
    self._batch_setitems(obj.iteritems())
  File "C:Python27libpickle.py", line 681, in _batch_setitems
    save(v)
  File "C:Python27libpickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:Python27libpickle.py", line 548, in save_tuple
    save(element)
  File "C:Python27libpickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:Python27libpickle.py", line 725, in save_inst
    save(stuff)
  File "C:Python27libpickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:Python27libpickle.py", line 649, in save_dict
    self._batch_setitems(obj.iteritems())
  File "C:Python27libpickle.py", line 681, in _batch_setitems
    save(v)
  File "C:Python27libpickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)
  File "C:Python27libpickle.py", line 419, in save_reduce
    save(state)
  File "C:Python27libpickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:Python27libpickle.py", line 548, in save_tuple
    save(element)
  File "C:Python27libpickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:Python27libpickle.py", line 649, in save_dict
    self._batch_setitems(obj.iteritems())
  File "C:Python27libpickle.py", line 681, in _batch_setitems
    save(v)
  File "C:Python27libpickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)
  File "C:Python27libpickle.py", line 396, in save_reduce
    save(cls)
  File "C:Python27libpickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:Python27libpickle.py", line 748, in save_global
    (obj, module, name))
PicklingError: Can't pickle <type 'cStringIO.StringO'>: it's not found as cStringIO.StringO
ReadingTraceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:Python27libmultiprocessingforking.py", line 347, in main
    self = load(from_parent)
  File "C:Python27libpickle.py", line 1378, in load
    return Unpickler(file).load()
  File "C:Python27libpickle.py", line 858, in load
    dispatch[key](self)
  File "C:Python27libpickle.py", line 880, in load_eof
    raise EOFError
EOFError

Handling request started Mon Oct 10 14:44:41 2011
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 65047)
Traceback (most recent call last):
  File "C:Python27libSocketServer.py", line 284, in _handle_request_noblock
    self.process_request(request, client_address)
  File "C:BarikUCS automationeclipse_workspaceUCSPython_HTTPsrcthreading_with_httpproblem_multiprocessing.py", line 46, in process_request
    self.finish_request(request, client_address)
  File "C:Python27libSocketServer.py", line 323, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "C:Python27libSocketServer.py", line 639, in __init__
    self.handle()
  File "C:BarikUCS automationeclipse_workspaceUCSPython_HTTPsrcthreading_with_httpproblem_multiprocessing.py", line 32, in handle
    t.start()
  File "C:Python27libmultiprocessingprocess.py", line 104, in start
    self._popen = Popen(self)
  File "C:Python27libmultiprocessingforking.py", line 244, in __init__
    dump(process_obj, to_child, HIGHEST_PROTOCOL)
  File "C:Python27libmultiprocessingforking.py", line 167, in dump
    ForkingPickler(file, protocol).dump(obj)
  File "C:Python27libpickle.py", line 224, in dump
    self.save(obj)
  File "C:Python27libpickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)
  File "C:Python27libpickle.py", line 419, in save_reduce
    save(state)
  File "C:Python27libpickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:Python27libpickle.py", line 649, in save_dict
    self._batch_setitems(obj.iteritems())
  File "C:Python27libpickle.py", line 681, in _batch_setitems
    save(v)
  File "C:Python27libpickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:Python27libpickle.py", line 548, in save_tuple
    save(element)
  File "C:Python27libpickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:Python27libpickle.py", line 725, in save_inst
    save(stuff)
  File "C:Python27libpickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:Python27libpickle.py", line 649, in save_dict
    self._batch_setitems(obj.iteritems())
  File "C:Python27libpickle.py", line 681, in _batch_setitems
    save(v)
  File "C:Python27libpickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)
  File "C:Python27libpickle.py", line 419, in save_reduce
    save(state)
  File "C:Python27libpickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:Python27libpickle.py", line 548, in save_tuple
    save(element)
  File "C:Python27libpickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:Python27libpickle.py", line 649, in save_dict
    self._batch_setitems(obj.iteritems())
  File "C:Python27libpickle.py", line 681, in _batch_setitems
    save(v)
  File "C:Python27libpickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)
  File "C:Python27libpickle.py", line 396, in save_reduce
    save(cls)
  File "C:Python27libpickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:Python27libpickle.py", line 748, in save_global
    (obj, module, name))
PicklingError: Can't pickle <type 'cStringIO.StringO'>: it's not found as cStringIO.StringO
----------------------------------------
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:Python27libmultiprocessingforking.py", line 347, in main
    self = load(from_parent)
  File "C:Python27libpickle.py", line 1378, in load
    return Unpickler(file).load()
  File "C:Python27libpickle.py", line 858, in load
    dispatch[key](self)
  File "C:Python27libpickle.py", line 880, in load_eof
    raise EOFError
EOFError
  

Любая помощь или альтернатива?

Комментарии:

1. Он должен распечатать, какая строка вызвала исключение. Можете ли вы пройти трассировку?

2. Добавлено полное ИСКЛЮЧЕНИЕ!! Здесь недостаточно места