python - 同时在python中运行多个服务器(线程)

标签 python python-2.7 web.py yowsup

我在 python 中有 2 个服务器我想将它们混合在一个 .py 中并一起运行:

服务器.py:

import logging, time, os, sys
from yowsup.layers import YowLayerEvent, YowParallelLayer
from yowsup.layers.auth import AuthError
from yowsup.layers.network import YowNetworkLayer
from yowsup.stacks.yowstack import YowStackBuilder

from layers.notifications.notification_layer import NotificationsLayer
from router import RouteLayer

class YowsupEchoStack(object):
    def __init__(self, credentials):
        "Creates the stacks of the Yowsup Server,"
        self.credentials = credentials
        stack_builder = YowStackBuilder().pushDefaultLayers(True)

        stack_builder.push(YowParallelLayer([RouteLayer, NotificationsLayer]))
        self.stack = stack_builder.build()
        self.stack.setCredentials(credentials)

    def start(self):
        self.stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT))
        try:
            logging.info("#" * 50)
            logging.info("\tServer started. Phone number: %s" % self.credentials[0])
            logging.info("#" * 50)
            self.stack.loop(timeout=0.5, discrete=0.5)
        except AuthError as e:
            logging.exception("Authentication Error: %s" % e.message)
            if "<xml-not-well-formed>" in str(e):
                os.execl(sys.executable, sys.executable, *sys.argv)
        except Exception as e:
            logging.exception("Unexpected Exception: %s" % e.message)

if __name__ == "__main__":
    import sys
    import config

    logging.basicConfig(stream=sys.stdout, level=config.logging_level, format=config.log_format)
    server = YowsupEchoStack(config.auth)
    while True:
        # In case of disconnect, keeps connecting...
        server.start()
        logging.info("Restarting..")

应用.py:

import web

urls = (
  '/', 'index'
)

app = web.application(urls, globals())

class index:
    def GET(self):
        greeting = "Hello World"
        return greeting

if __name__ == "__main__":
    app.run()

我想从单个 .py 文件一起运行这两个。 如果我尝试从一个文件运行它们,两者中的任何一个都会启动,而另一个仅在第一个文件完成工作时才启动。

如何在 python 中一起运行 2 个服务器

最佳答案

import thread

def run_app1():
    #something goes here

def run_app2():
    #something goes here


if __name__=='__main__':
    thread.start_new_thread(run_app1)
    thread.start_new_thread(run_app2)

如果你需要将参数传递给你可以做的函数:

thread.start_new_thread(run_app1, (arg1,arg2,....))

如果你想在你的线程中有更多的控制,你可以去:

import threading
def app1():
    #something here

def app2():
    #something here

if __name__=='__main__':
    t1 = threading.Thread(target=app1)
    t2 = threading.Thread(target=app2)
    t1.start()
    t2.start()

如果你需要传递参数,你可以去:

t1 = threading.Thread(target=app1, args=(arg1,arg2,arg3.....))

线程与线程之间有什么区别?线程是比线程更高级别的模块,在 3.x 中,线程已重命名为 _thread ... 更多信息:http://docs.python.org/library/threading.html但我想那是另一个问题。

因此在您的情况下,只需创建一个运行第一个脚本和第二个脚本的函数,然后生成线程来运行它们。

关于python - 同时在python中运行多个服务器(线程),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39350300/

相关文章:

python - 基于示例代码在 View 中保存的最佳实践

python - 为什么我在尝试抓取特定网站时会收到 "Connection aborted"错误?

python - 如何消除python init函数中的以下代码重复

python - 多核批处理

python - web.py - WalkerError : ('unexpected node type' , 339)

用于读取文件记录的Python modbus lib

python - 停止 cmdloop() 从 python 中的输入行中去除空格

python - 加载yaml时如何使用自定义字典类?

python - 如何在 webpy 中提供文件?

mysql - 使用 web.py 有效地使用查询结果