简体   繁体   English

使用Bottle.py服务器进行线程化

[英]Threading with Bottle.py Server

I'm having an issue with threading that I can't solve in any way I've tried. 我遇到了线程问题,无论如何我都无法解决。 I searched in StackOverflow too, but all I could find was cases that didn't apply to me, or explanations that I didn't understand. 我也在StackOverflow中进行了搜索,但是我只能找到不适用于我的情况或我不理解的解释。

I'm trying to build an app with BottlePy, and one of the features I want requires a function to run in background. 我正在尝试使用BottlePy构建应用程序,而我想要的功能之一需要一个在后台运行的功能。 For this, I'm trying to make it run in a thread. 为此,我试图使其在线程中运行。 However, when I start the thread, it runs twice . 但是,当我启动线程时,它将运行两次

I've read in some places that it would be possible to check if the function was in the main script or in a module using if __name__ == '__main__': , however I'm not able to do this, since __name__ is always returning the name of the module. 我已经在某些地方阅读过,可以使用if __name__ == '__main__':来检查函数是在主脚本中还是在模块中,但是我无法执行此操作,因为__name__总是返回模块的名称。

Below is an example of what I'm doing right now. 下面是我现在正在做的一个例子。

The main script: 主要脚本:

# main.py
from MyClass import *
from bottle import *

arg = something

myObject = Myclass(arg1)

app = Bottle()
app.run('''bottle args''')

The class: 班级:

# MyClass.py
import threading
import time

class MyClass:
    def check_list(self, theList, arg1):
        a_list = something()
        time.sleep(5)
        self.check_list(a_list, arg1)

    def __init__(self, arg1):
        if __name__ == '__main__':
            self.a_list = arg.returnAList()
            t = threading.Thread(target=self.check_list, args=(a_list, arg1))

So what I intend here is to have check_list running in a thread all the time, doing something and waiting some seconds to run again. 所以我在这里打算让check_list一直在线程中运行,做一些事情并等待几秒钟再次运行。 All this so I can have the list updated, and be able to read it with the main script. 所有这些使我可以更新列表,并能够使用主脚本读取它。

Can you explain to me what I'm doing wrong, why the thread is running twice, and how can I avoid this? 您能告诉我我做错了什么,为什么线程运行两次,如何避免这种情况?

This works fine: 这工作正常:

import threading
import time

class MyClass:
    def check_list(self, theList, arg1):
        keep_going=True
        while keep_going:
            print("check list")
            #do stuff
            time.sleep(1)

    def __init__(self, arg1):
        self.a_list = ["1","2"]
        t = threading.Thread(target=self.check_list, args=(self.a_list, arg1))
        t.start()

myObject = MyClass("something")

Figured out what was wrong thanks to the user Weeble's comment. 由于用户Weeble的评论,找出了问题所在。 When he said 'something is causing your main.py to run twice' I remembered that Bottle has an argument that is called 'reloader'. 当他说“某事导致您的main.py运行两次”时,我记得Bottle带有一个称为“ reloader”的参数。 When set to True , this will make the application load twice , and thus the thread creation is run twice as well. 设置为True时这将使应用程序加载两次 ,因此线程创建也将运行两次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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