如何从另一个脚本将Bottle作为守护程序启动? [英] How to start Bottle as a daemon from another script?

查看:83
本文介绍了如何从另一个脚本将Bottle作为守护程序启动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 BottlePy 用作从另一个脚本启动的守护程序,但是我遇到了问题将独立脚本(webserver.py)转换为一个类.我的网络服务器的独立版本在下面可以正常运行:

I would like to use BottlePy as a daemon started from another script and I have issues turning a standalone script (webserver.py) into a class. The standalone version of my webserver below works fine:

import bottle

@bottle.get('/hello')
def hello():
    return 'Hello World'

@bottle.error(404)
def error404(error):
    return 'error 404'

bottle.run(host='localhost', port=8080)

我的意图是从下面的主脚本中启动它

My intent is now to start it from the main script below as

from webserver import WebServer
from multiprocessing import Process

def start_web_server():
    # initialize the webserver class
    WebServer()

# mainscript.py operations
p = Process(target=start_web_server)
p.daemon = True
p.start()
# more operations

其中WebServer()将位于天真的修改过的webserver.py中:

where WebServer() would be in the naively now-modified webserver.py:

import bottle

class WebServer():
    def __init__(self):
        bottle.run(host='localhost', port=8080)

    @bottle.get('/hello')
    def hello(self):
        return 'Hello World'

    @bottle.error(404)
    def error404(self, error):
        return 'error 404'

有效的方法:整个过程开始了,网络服务器正在监听

What works: the whole thing starts and the webserver is listening

什么不起作用:致电http://localhost:8080/hello

127.0.0.1 - - [11/Dec/2013 10:16:23] "GET /hello HTTP/1.1" 500 746
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\bottle.py", line 764, in _handle
    return route.call(**args)
  File "C:\Python27\lib\site-packages\bottle.py", line 1575, in wrapper
    rv = callback(*a, **ka)
TypeError: hello() takes exactly 1 argument (0 given)

我的问题是:

  • 我希望传递给hello()error404()的参数是什么?
  • 我应该怎么做才能参数化@bottle.get('/hello')?我想使用@bottle.get(hello_url)之类的东西,但是hello_url = '/hello'应该在哪里初始化? (@bottle.get不知道self.hello_url)
  • what kind of parameters am I expected to pass to hello() and error404()?
  • what should I do in order to parametrize @bottle.get('/hello')? I would like to have something like @bottle.get(hello_url) but where should hello_url = '/hello' be initialized? (self.hello_url is unknown to @bottle.get)

编辑:在准备该问题的分支以处理问题2(关于参数化)时,我顿悟了一下,并尝试了可行的明显解决方案(下面的代码).我还不习惯于使用类,因此我没有反应将变量添加到类的范围内.

EDIT: while preparing a fork of this question to handle question 2 (about the parametrization) I had an epiphany and tried the obvious solution which works (code below). I am not that used to classes yet so I did not have the reflex to add the variable in the scope of the class.

# new code with the path as a parameter
class WebServer():

    myurl = '/hello'

    def __init__(self):
        bottle.run(host='localhost', port=8080, debug=True)

    @bottle.get(myurl)
    def hello():
        return 'Hello World'

    @bottle.error(404)
    def error404(error):
        return 'error 404'

推荐答案

我应该传递给hello()和error404()什么样的参数?

what kind of parameters am I expected to pass to hello() and error404()?

简短的答案:无.只需删除self,它们就会开始起作用.

The short answer: none. Just remove self and they should start to work.

@bottle.get('/hello')
def hello():
    return 'Hello World'

@bottle.error(404)
def error404(error):
    return 'error 404'


我应该怎么做才能参数化@ bottle.get('/hello')?一世 想要有类似@ bottle.get(hello_url)的东西,但是在哪里 是否应初始化hello_url ='/hello'? (self.hello_url是未知的 到@ bottle.get)

what should I do in order to parametrize @bottle.get('/hello')? I would like to have something like @bottle.get(hello_url) but where should hello_url = '/hello' be initialized? (self.hello_url is unknown to @bottle.get)

我可以用几种不同的方式解释这一点,所以我不确定如何为您提供帮助.但是,由于这是一个完全独立的问题(范围可能更大),请考虑在一个新的单独的SO问题中提问.

I can interpret this a couple of different ways, so I'm not sure how to help you. But since this is a completely separate question (with potentially a much larger scope), please consider asking it in a new, separate SO question.

这篇关于如何从另一个脚本将Bottle作为守护程序启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆