W
W
wutiarn2014-02-16 19:13:26
Django
wutiarn, 2014-02-16 19:13:26

How can you pass a reference to a class instance to other modules in order to interact with it?

Good afternoon. There is a site (more precisely, a control panel) on Django. When the web server is started, a daemon must be started (an instance of a class is created that, when initialized, starts an infinite loop in a new thread. This is currently happening in the models.py of the application). In the future (at the request of the user from the panel), it is necessary to transfer / receive data from this daemon using its methods from the page handler (suppose /save).
Accordingly, the question arises: how can you pass a reference to an instance of a class (daemon) to other modules (page handler) in order to interact with it.
If this is not possible, then describe how you would implement such a thing. It is in the context of django.
Ps Do not offer the option to put the necessary data / queries / etc in the database, because this whole system works on raspberry pi and I don’t want to touch the SD card once again. In addition, there are operations that require an instant reaction from the demon.
Thank you in advance for your help.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry, 2014-02-17
@wutiarn

Don't think of threads as objects, because they are not. A thread is a thread of execution that, during initialization, receives a function to be executed. After initialization, this is a separate mini-process. It is impossible to call a thread method: it simply does not have them. You can call the method of the handle (of the object that manages the thread), but not the thread itself. The difference between threads and processes is that threads share an address space with the process that spawned them. This means that each thread has access to the data of the process and other threads.
Now about the solutions. I see several of them:
1. Before starting the thread, create a proxy object, an instance of a class that will describe a data structure suitable for you. Depending on the task, it may be useful to look atQueue is thread-safe out of the box. After that, just pass a reference to this object to the constructor of the thread and communicate through it. The thread writes to this object - django reads from it, and vice versa. The queue model for such tasks is the best fit, because it is impossible to guarantee that the thread will pick up the task immediately upon its appearance, and another task will not overwrite the previous one. You need to create a proxy object at a point where the parent process does not exit until the thread stops, otherwise you risk losing control of the thread or catching "weird" errors.
2: If there is a need to set different tasks for the thread, it may be reasonable to move their storage to external services, such as redis. It is very fast and will not create a significant overhead even on a raspberry. You can communicate with him through this package . If you want to save on tcp traffic, run redis on a unix socket.
This option will potentially save you the headache of task synchronization.
3: If there is a possibility and there is enough memory for more than 1 thread - RQ . This is a lightweight task queue manager for python. In fact, what you are trying to implement.

D
Dmitry, 2014-02-16
@EvilsInterrupt

In python, it is customary that every method takes self as the first parameter. This is the reference to the class object.

A
alz, 2014-02-17
@alz

Usually, when deploying a django project (the apache + mod_wsgi and nginx + uwsgi bundles are quite popular), several processes are launched that serve user requests. Will you have one process, or will you have multiple "daemons"? Move the daemon to a separate application and pull it through XMLRPC

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question