简体   繁体   中英

Python multiprocessing / Queue with cherrypy

I am tying to do a python code with cherrypy. The objective is to call a script from UI and while the script generates the logs on console print it on the UI (Iframe / div). I am using CherryPy-3.2.4 and Python 2.7. Following is the sample code I have written which is refusing to work.

TestProcess.py

from multiprocessing import Process, Queue
import subprocess
import os, os.path
from string import Template
import cherrypy
from multiprocessing import Process, Queue
import HTML
import TestScript

class TestProcess(object):
    PID=0
    jquery_url = 'C:\CherryPy\TestScripts\jquery\jquery-1.11.1.js'
    q=Queue()

    @cherrypy.expose
    def index(self):
        html = """\
            <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
            <HTML>
             <HEAD>
              <TITLE> Test Doc </TITLE>
                <script type="text/javascript" src="/static/jq/jquery-1.11.1.js"></script>
             </HEAD>
             <BODY>
                 <BR/>
                     <h3>Test utility</h3>
                      <form id="ping_form" target="console_iframe" method="post" action="/f">
                              <button id="ping" type="submit">Upgrade Installed Releases</button>
                      </form>

                      <BR/>
                      <iframe name="console_iframe" frameborder="1" width="1200"/>
             </BODY>
            </HTML>
            """
    t = Template(html)
    page = t.substitute(jquery_url=self.jquery_url)
    return page

def f1():
    print "*********** TEST **********************"
    q.put([42, None, 'hello'])

@cherrypy.expose
def f(self, **kw):
    q=Queue()
    ts = TestScript.TestScript()
    p = Process(target=ts.testCallFunction, args=(q,))
    p.start()
    #print q.get()    # prints "[42, None, 'hello']"
    p.join()
    print "Test F"

    def run_command():
      # The yeilds here are the key to keeping things streaming
      yield '<style>body {font-family: monospace;}</style>'
      while(p.is_alive()):
        while(not q.empty()):
          yield q.get_nowait()   
      while(not q.empty()):
        yield q.get_nowait()   
    return run_command()
if __name__ == '__main__':
  conf = {
    '/': {
         'tools.sessions.on': True,
         'tools.staticdir.root': os.path.abspath(os.getcwd())
     },
     '/static': {
         'tools.staticdir.on': True,
         'tools.staticdir.dir': './public'
     }
   }
  q = Queue()
  cherrypy.server.socket_host = '10.49.69.103'
  cherrypy.config.update({
  'log.screen':True,
  'tools.sessions.on': True,
  'checker.on':False
})

cherrypy.tree.mount(TestProcess(), config=None)
cherrypy.engine.start()
cherrypy.engine.block()
#cherrypy.quickstart(TestProcess(), '/', conf)

Class TestScript

class TestScript(object):

q=Queue()

def write_queue(self,data):
    print "*********** TEST **********************"
    scroll_to_bottom = '<script type="text/javascript">window.scrollBy(0,50);</script>'
    if data == '\n':
      self.q.put("\n<br />%s" % scroll_to_bottom) # include the iframe scroll fix
    else:
      self.q.put(data)

def testCallFunction(self, q):
    #proc = subprocess.Popen(['python','CreateLog.py'],stdout=subprocess.PIPE)
    cmd = subprocess.Popen(['python','CreateLog.py'], shell=True, stdout=subprocess.PIPE)
    while True:
      data = cmd.stdout.read(1)   # Alternatively proc.stdout.read(1024)
      if len(data) == 0:
        break
      self.write_queue(data)   # sys.stdout.buffer.write(data) on Python 3.x
    #print "*********** TEST **********************"

script CreateLog.py

#filters output
import time
i = 0
while (i<=10):
   print hex(i)*512
   i += 1
   time.sleep(0.5)

Error That I get while running it

[24/Jul/2014:16:59:10] ENGINE Bus STARTING
[24/Jul/2014:16:59:10] ENGINE Started monitor thread 'Autoreloader'.
[24/Jul/2014:16:59:10] ENGINE Started monitor thread '_TimeoutMonitor'.
[24/Jul/2014:16:59:15] ENGINE Error in 'start' listener <bound method Server.sta
rt of <cherrypy._cpserver.Server object at 0x01583390>>
Traceback (most recent call last):
 File "c:\Python27\lib\site-packages\cherrypy\process\wspbus.py", line 197, in
publish
output.append(listener(*args, **kwargs))
 File "c:\Python27\lib\site-packages\cherrypy\_cpserver.py", line 151, in start

ServerAdapter.start(self)
File "c:\Python27\lib\site-packages\cherrypy\process\servers.py", line 168, in
 start
wait_for_free_port(*self.bind_addr)
 File "c:\Python27\lib\site-packages\cherrypy\process\servers.py", line 412, in
wait_for_free_port
 raise IOError("Port %r not free on %r" % (port, host))
 IOError: Port 8080 not free on '10.49.69.103'

 [24/Jul/2014:16:59:15] ENGINE Shutting down due to error in start listener:
Traceback (most recent call last):
 File "c:\Python27\lib\site-packages\cherrypy\process\wspbus.py", line 235, in
 start
self.publish('start')
 File "c:\Python27\lib\site-packages\cherrypy\process\wspbus.py", line 215, in
 publish
raise exc
 ChannelFailures: IOError("Port 8080 not free on '10.49.69.103'",)

What am I doing wrong ?

See 16.6.3.2. Windows :

Make sure that the main module can be safely imported by a new Python interpreter without causing unintended side effects (such a starting a new process).

You did not protect the last lines of TestProcess.py with if __name__ == '__main__': , so the Process attempts to run another server.

Port 8080 is not free, so the HTTP server can't listen on it. Try netstat -anpt to see what's using that port, or try "server.socket_port: 8081" or some other port that is free.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM