Python 防止代码在导入时运行
当CherryPy web应用程序启动时导入模块,而不是创建新的Python 防止代码在导入时运行,python,multiprocessing,cherrypy,python-multiprocessing,Python,Multiprocessing,Cherrypy,Python Multiprocessing,当CherryPy web应用程序启动时导入模块,而不是创建新的进程时,如何获取一些代码来运行 我的CherryPy应用程序遵循以下模式: Main.py from Api1.Api1 import Api1 from Api2.Api2 import Api2 config = {'global': {'server.socket_host': '0.0.0.0'}} class Root(): global config def __init__(self):
进程时,如何获取一些代码来运行
我的CherryPy应用程序遵循以下模式:
Main.py
from Api1.Api1 import Api1
from Api2.Api2 import Api2
config = {'global': {'server.socket_host': '0.0.0.0'}}
class Root():
global config
def __init__(self):
self.app1 = App1('app1')
self.app2 = App2('app2')
config.update(self.app1.config)
config.update(self.app2.config)
if __name__ == '__main__':
cherrypy.quickstart(Root(), '/', config)
def every_hour():
[...]
Monitor(cherrypy.engine, every_hour, frequency=60 * 60).start()
db = peewee.SqliteDatabase('app1.db', threadlocals=True)
class App1():
def __init__(self, root_dir):
self.root_dir = root_dir
my_path = os.path.dirname(__file__)
self.config = {'/%s/css' % root_dir: {'tools.staticdir.on': True, 'tools.staticdir.dir': '%s\\css' % my_path},
'/%s/img' % root_dir: {'tools.staticdir.on': True, 'tools.staticdir.dir': '%s\\img' % my_path}}
from App1.App1 import App1, app1_init
from App2.App2 import App2
config = {'global': {'server.socket_host': '0.0.0.0'}}
class Root():
global config
def __init__(self):
self.app1 = App1('app1')
self.app2 = App2('app2')
config.update(self.app1.config)
config.update(self.app2.config)
if __name__ == '__main__':
app1_init()
cherrypy.quickstart(Root(), '/', config)
def every_hour():
[...]
db = None
def app1_init():
global db
Monitor(cherrypy.engine, every_hour, frequency=60 * 60).start()
db = peewee.SqliteDatabase('app1.db', threadlocals=True)
class App1():
def __init__(self, root_dir):
self.root_dir = root_dir
my_path = os.path.dirname(__file__)
self.config = {'/%s/css' % root_dir: {'tools.staticdir.on': True, 'tools.staticdir.dir': '%s\\css' % my_path},
'/%s/img' % root_dir: {'tools.staticdir.on': True, 'tools.staticdir.dir': '%s\\img' % my_path}}
这里的if uuuuu name uuu='.\uuuuu main uuu'.
做它的工作:uuu name uu='.\uuuu main uu='.\uuuu mp uu main
当服务器启动时,创建一个新的进程时,所以cherrypy.quickstart()
只发生一次
App1\App1.py
from Api1.Api1 import Api1
from Api2.Api2 import Api2
config = {'global': {'server.socket_host': '0.0.0.0'}}
class Root():
global config
def __init__(self):
self.app1 = App1('app1')
self.app2 = App2('app2')
config.update(self.app1.config)
config.update(self.app2.config)
if __name__ == '__main__':
cherrypy.quickstart(Root(), '/', config)
def every_hour():
[...]
Monitor(cherrypy.engine, every_hour, frequency=60 * 60).start()
db = peewee.SqliteDatabase('app1.db', threadlocals=True)
class App1():
def __init__(self, root_dir):
self.root_dir = root_dir
my_path = os.path.dirname(__file__)
self.config = {'/%s/css' % root_dir: {'tools.staticdir.on': True, 'tools.staticdir.dir': '%s\\css' % my_path},
'/%s/img' % root_dir: {'tools.staticdir.on': True, 'tools.staticdir.dir': '%s\\img' % my_path}}
from App1.App1 import App1, app1_init
from App2.App2 import App2
config = {'global': {'server.socket_host': '0.0.0.0'}}
class Root():
global config
def __init__(self):
self.app1 = App1('app1')
self.app2 = App2('app2')
config.update(self.app1.config)
config.update(self.app2.config)
if __name__ == '__main__':
app1_init()
cherrypy.quickstart(Root(), '/', config)
def every_hour():
[...]
db = None
def app1_init():
global db
Monitor(cherrypy.engine, every_hour, frequency=60 * 60).start()
db = peewee.SqliteDatabase('app1.db', threadlocals=True)
class App1():
def __init__(self, root_dir):
self.root_dir = root_dir
my_path = os.path.dirname(__file__)
self.config = {'/%s/css' % root_dir: {'tools.staticdir.on': True, 'tools.staticdir.dir': '%s\\css' % my_path},
'/%s/img' % root_dir: {'tools.staticdir.on': True, 'tools.staticdir.dir': '%s\\img' % my_path}}
在这里,同样的技巧不起作用,因为它总是“App1.App1”
,所以在服务器启动和进程(target=some\u func)时启动一个新的监视器,并创建一个新的数据库连接。start()
导入时将运行不在方法或类中的代码,因此,如果您有一个文件x.py,其中只显示:
print("I'm just a poor boy, I need no sympathy")
如果您有另一个类似您的文件导入此文件,您将看到此文本的打印,甚至在main或init启动之前
[编辑]读回我的答案可能有点笼统,但正因为如此,您的代码在类或函数之外:
Monitor(cherrypy.engine, every_hour, frequency=60 * 60).start()
db = peewee.SqliteDatabase('app1.db', threadlocals=True)
导入时也将运行。一个选项是将App1.py顶层的初始化代码移动到一个函数,然后从中调用该函数,如果
name\uuuuuu==“\uuuu main\uuuu”:
在main.py
中进行保护:
Main.py
from Api1.Api1 import Api1
from Api2.Api2 import Api2
config = {'global': {'server.socket_host': '0.0.0.0'}}
class Root():
global config
def __init__(self):
self.app1 = App1('app1')
self.app2 = App2('app2')
config.update(self.app1.config)
config.update(self.app2.config)
if __name__ == '__main__':
cherrypy.quickstart(Root(), '/', config)
def every_hour():
[...]
Monitor(cherrypy.engine, every_hour, frequency=60 * 60).start()
db = peewee.SqliteDatabase('app1.db', threadlocals=True)
class App1():
def __init__(self, root_dir):
self.root_dir = root_dir
my_path = os.path.dirname(__file__)
self.config = {'/%s/css' % root_dir: {'tools.staticdir.on': True, 'tools.staticdir.dir': '%s\\css' % my_path},
'/%s/img' % root_dir: {'tools.staticdir.on': True, 'tools.staticdir.dir': '%s\\img' % my_path}}
from App1.App1 import App1, app1_init
from App2.App2 import App2
config = {'global': {'server.socket_host': '0.0.0.0'}}
class Root():
global config
def __init__(self):
self.app1 = App1('app1')
self.app2 = App2('app2')
config.update(self.app1.config)
config.update(self.app2.config)
if __name__ == '__main__':
app1_init()
cherrypy.quickstart(Root(), '/', config)
def every_hour():
[...]
db = None
def app1_init():
global db
Monitor(cherrypy.engine, every_hour, frequency=60 * 60).start()
db = peewee.SqliteDatabase('app1.db', threadlocals=True)
class App1():
def __init__(self, root_dir):
self.root_dir = root_dir
my_path = os.path.dirname(__file__)
self.config = {'/%s/css' % root_dir: {'tools.staticdir.on': True, 'tools.staticdir.dir': '%s\\css' % my_path},
'/%s/img' % root_dir: {'tools.staticdir.on': True, 'tools.staticdir.dir': '%s\\img' % my_path}}
App1.py
from Api1.Api1 import Api1
from Api2.Api2 import Api2
config = {'global': {'server.socket_host': '0.0.0.0'}}
class Root():
global config
def __init__(self):
self.app1 = App1('app1')
self.app2 = App2('app2')
config.update(self.app1.config)
config.update(self.app2.config)
if __name__ == '__main__':
cherrypy.quickstart(Root(), '/', config)
def every_hour():
[...]
Monitor(cherrypy.engine, every_hour, frequency=60 * 60).start()
db = peewee.SqliteDatabase('app1.db', threadlocals=True)
class App1():
def __init__(self, root_dir):
self.root_dir = root_dir
my_path = os.path.dirname(__file__)
self.config = {'/%s/css' % root_dir: {'tools.staticdir.on': True, 'tools.staticdir.dir': '%s\\css' % my_path},
'/%s/img' % root_dir: {'tools.staticdir.on': True, 'tools.staticdir.dir': '%s\\img' % my_path}}
from App1.App1 import App1, app1_init
from App2.App2 import App2
config = {'global': {'server.socket_host': '0.0.0.0'}}
class Root():
global config
def __init__(self):
self.app1 = App1('app1')
self.app2 = App2('app2')
config.update(self.app1.config)
config.update(self.app2.config)
if __name__ == '__main__':
app1_init()
cherrypy.quickstart(Root(), '/', config)
def every_hour():
[...]
db = None
def app1_init():
global db
Monitor(cherrypy.engine, every_hour, frequency=60 * 60).start()
db = peewee.SqliteDatabase('app1.db', threadlocals=True)
class App1():
def __init__(self, root_dir):
self.root_dir = root_dir
my_path = os.path.dirname(__file__)
self.config = {'/%s/css' % root_dir: {'tools.staticdir.on': True, 'tools.staticdir.dir': '%s\\css' % my_path},
'/%s/img' % root_dir: {'tools.staticdir.on': True, 'tools.staticdir.dir': '%s\\img' % my_path}}
这不是很理想,因为任何导入App1
的东西都需要知道如何调用App1\u init
,但它确实解决了这个问题。我知道。。。因此我的问题是