开发者

How do I pass values from one class to another; each running potentially on different threads?

开发者 https://www.devze.com 2023-03-27 22:04 出处:网络
I have a file called filterspecs.py that contains 3 things: tls = threading.local() class A which inherits from django.contrib.admin.views.main.ChangeList

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

  1. tls = threading.local()
  2. class A which inherits from django.contrib.admin.views.main.ChangeList
  3. class B which inherits from django.contrib.admin.filterspecs.FilterSpec

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).

Attempt开发者_JAVA技巧 #1 fails when using WSGI (daemon) mode. In class A, the list is added to the threadlocal.

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

But in class B, the following returns False:

2.    hasattr(tls, 'list_display')

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

manage.py runserver

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

What other ways can I solve this problem ?


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.)

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号