如何将值从一个类传递到另一个类;每个可能在不同的线程上运行?

时间:2022-02-18 23:47:13

I have a file called filterspecs.py that contains 3 things:

我有一个名为filterspecs.py的文件,其中包含3件事:

  1. tls = threading.local()
  2. tls = threading.local()

  3. class A which inherits from django.contrib.admin.views.main.ChangeList
  4. 继承自django.contrib.admin.views.main.ChangeList的A类

  5. class B which inherits from django.contrib.admin.filterspecs.FilterSpec
  6. 继承自django.contrib.admin.filterspecs.FilterSpec的B类

Goal: I want to pass a value, a list, available to an instance of A to an instance of B. Since the lifecycle of A and B are managed by the framework (Django), I cannot think of private way to pass the data between the instances (Using a Queue would be an overkill).

目标:我想将A的实例可用的值,列表传递给B的实例。由于A和B的生命周期由框架(Django)管理,我无法想到私有方式传递数据实例之间(使用队列将是一个过度杀伤)。

Attempt #1 fails when using WSGI (daemon) mode. In class A, the list is added to the threadlocal.

使用WSGI(守护程序)模式时尝试#1失败。在类A中,列表被添加到threadlocal中。

1.    tls.list_display = ['foo', 'bar']

But in class B, the following returns False:

但是在B类中,以下返回False:

2.    hasattr(tls, 'list_display')

Just for comparison sake, this works outside of apache/mod_wsgi if I run it via

仅仅为了比较,如果我通过它运行它,这在apache / mod_wsgi之外工作

manage.py runserver

I looked at the logs and discovered that different threads were executing lines 1 & 2.

我查看了日志,发现不同的线程正在执行第1行和第2行。

What other ways can I solve this problem ?

还有哪些方法可以解决这个问题?

1 个解决方案

#1


1  

It sounds like you want to share data between not just two classes, but between two completely different HTTP requests. HTTP is stateless -- Apache is not designed to accommodate stateful web applications. You must save the data to a database and read it back in the second request. (Or you can run your web application in a separate process and manage the state yourself -- which essentially adds "database programmer" to your job description. This is not recommended.)

听起来你想要在不仅仅是两个类之间共享数据,而是在两个完全不同的HTTP请求之间共享数据。 HTTP是无状态的 - Apache不是为容纳有状态的Web应用程序而设计的。您必须将数据保存到数据库并在第二个请求中将其读回。 (或者您可以在单独的流程中运行Web应用程序并自行管理状态 - 这实际上会在您的工作描述中添加“数据库程序员”。不建议这样做。)

Your two pieces of code might not just be running in different threads, they might be running in different processes.

您的两段代码可能不只是在不同的线程中运行,它们可能在不同的进程中运行。

#1


1  

It sounds like you want to share data between not just two classes, but between two completely different HTTP requests. HTTP is stateless -- Apache is not designed to accommodate stateful web applications. You must save the data to a database and read it back in the second request. (Or you can run your web application in a separate process and manage the state yourself -- which essentially adds "database programmer" to your job description. This is not recommended.)

听起来你想要在不仅仅是两个类之间共享数据,而是在两个完全不同的HTTP请求之间共享数据。 HTTP是无状态的 - Apache不是为容纳有状态的Web应用程序而设计的。您必须将数据保存到数据库并在第二个请求中将其读回。 (或者您可以在单独的流程中运行Web应用程序并自行管理状态 - 这实际上会在您的工作描述中添加“数据库程序员”。不建议这样做。)

Your two pieces of code might not just be running in different threads, they might be running in different processes.

您的两段代码可能不只是在不同的线程中运行,它们可能在不同的进程中运行。