从单独的线程访问 app_globals

发布于 2024-11-29 09:37:02 字数 511 浏览 0 评论 0原文

我想在我的 Pylons 项目中创建一个单独的线程来进行繁重的计算,通过 python Queue 对象与之交互。问题是我需要访问 app_globals 对象。当我尝试这样做时,我得到了

TypeError: No object (name: app_globals) has been registered for this thread

我也尝试以这种方式访问​​app_globals:

app_globals = config.get('pylons.app_globals')
self.cadfile = app_globals.pm.get_upload_path(session, 'cadfile')

但随后我收到另一个错误:

AttributeError: 'NoneType' object has no attribute 'pm'

如何从单独的线程访问app_globals?

I'd like to create a separate thread for heavy computations in my Pylons project interacting with it through python Queue object. The problem is that I need to get to the app_globals object. When I try to do that, i get a

TypeError: No object (name: app_globals) has been registered for this thread

I also tried to access app_globals in this way:

app_globals = config.get('pylons.app_globals')
self.cadfile = app_globals.pm.get_upload_path(session, 'cadfile')

but then I get another error:

AttributeError: 'NoneType' object has no attribute 'pm'

How should I access app_globals from a separate thread?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里人 2024-12-06 09:37:02

可能您必须将 app_globals 作为启动参数传递给线程或通过队列传递它?我不确定,但我认为 app_globals 实现为 threading.local

例如。

class MyWorker(threading.Thread):
    def __init__(self, local_app_globals, *args, **kwargs):
        self.app_globals=local_app_globals
        super(MyWorker, self).__init__(*args, **kwargs)

    def run():
        #use self.app_globals as app_globals

t=MyWorker(app_globals)
t.start()

May be you must pass app_globals to thread as a start parameter or pass it through a queue? I not shure, but I think that app_globals implemented as threading.local

Eg.

class MyWorker(threading.Thread):
    def __init__(self, local_app_globals, *args, **kwargs):
        self.app_globals=local_app_globals
        super(MyWorker, self).__init__(*args, **kwargs)

    def run():
        #use self.app_globals as app_globals

t=MyWorker(app_globals)
t.start()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文