简体   繁体   English

我们如何运行 2 个并行共享公共变量的 python 脚本?

[英]How can we run 2 python scripts that share a common variable in parallel?

First let me explain the scenario.首先让我解释一下场景。

My project is built using Django.我的项目是使用 Django 构建的。

I have the frontend wherein we can upload the image of a person.我有前端,我们可以在其中上传一个人的图像。 That image will be stored in our local machine in (media folder).该图像将存储在我们本地计算机的(媒体文件夹)中。

Ex: If the image name is jon.jpeg , then it will be stored as例如:如果图像名称是jon.jpeg ,那么它将被存储为

media
-jon
--jon.jpeg

If another image is uploaded, say bob.jpg , then the structure will be like:如果上传了另一张图片,比如bob.jpg ,那么结构将如下所示:

media
-jon
--jon.jpeg
-bob
--bob.jpg

Now I have one Python script (say check.py ) in which we are running one infinite loop, and every 5 seconds it is checking the number of folders in the media folder.现在我有一个 Python 脚本(比如check.py ),我们在其中运行一个无限循环,并且每 5 秒检查一次媒体文件夹中的文件夹数量。 Also, we are maintaining one global variable in check.py that will keep track of the initial_count.此外,我们在check.py中维护一个全局变量,用于跟踪 initial_count。

Now I have another Python file running (ie camera.py ) and it has 2 functions, ie load_encodings and recognize in a class called Face_Recog .现在我有另一个 Python 文件正在运行(即camera.py ),它有两个功能,即load_encodings并在名为Face_Recog的 class 中recognize

load_encodings will convert the images in the media folder to feature encodings and it will store these encodings in a global list ( known_faces ) in the camera.py file. load_encodings会将媒体文件夹中的图像转换为特征编码,并将这些编码存储在camera.py文件中的全局列表 ( known_faces ) 中。

recognize will first extract the image from one camera frame and encode it, and then it will compare it with the encodings in known_faces . recognize将首先从一个相机帧中提取图像并对其进行编码,然后将其与known_faces中的编码进行比较。

Now whenever the number of folders in media changes, (if we upload the image from the frontend), then in our initial_count in check.py will change.现在,每当media中的文件夹数量发生变化时(如果我们从前端上传图像),那么check.py中的initial_count就会发生变化。 We have given a condition wherein if the initial_count value changes, it will call the encoding function of the camera.py file.我们给出了一个条件,如果initial_count值发生变化,它将调用camera.py文件的encoding function。

check.py

from camera import Face_Recog,object

initial_count = 0
while True:
    count = 0
    for path in os.listdir(path/to/media):
        count += 1
    global initial_count
    if count != initial_count:
        Face_Recog.load_encodings(object)
        initial_count = count
    sleep(5)

camera.py

known_faces = []

class Face_Recog(object):
    def load_encodings(self):
        temp = []
        #some code that will change the temp value , i.e it will load the image from media and then encode it and store the encoding in temp
        global known_faces
        known_faces = temp

    def recognize(self):
        while(True):
            #code that uses known_faces and recognizes


object = Face_Recog()
object.load_encodings()
object.recognize()

Now I am running these two files in 2 different shells.现在我在 2 个不同的 shell 中运行这两个文件。

Initially the media folder contains only jon , so known_faces will contain only encodings of jon .最初,媒体文件夹仅包含jon ,因此known_faces将仅包含jon的编码。

known_faces = ['jon']

and the recognize function will not recognize bob , it will only recognize jon .并且recognize function 不会识别bob ,它只会识别jon

(This is working.) (这是有效的。)

Now when I upload the image of bob , the initial_count value will change in check.py and it will call the load_encodings function of camera.py (it is calling it; I have checked using print statements).现在,当我上传bob的图像时, check.py中的initial_count值会发生变化,它会调用load_encodings的 load_encodings camera.py (它正在调用它;我已经使用打印语句进行了检查)。

Now this function will again encode the images present, ie jon and bob , and update the global variable, ie known_faces .现在这个 function 将再次编码存在的图像,即jonbob ,并更新全局变量,即known_faces

(It is updating, but only in the shell in which check.py is running, not in the shell running camera.py . Ie it is not reflecting the changes in the camera.py file's known_faces variable, which is running in a different shell instance). (It is updating, but only in the shell in which check.py is running, not in the shell running camera.py . Ie it is not reflecting the changes in the camera.py file's known_faces variable, which is running in a different shell实例)。

So even if it is changing the known_faces variable, the recognize function is not recognizing bob , ie the variable ( known_faces ) it is using is not updated automatically.因此,即使它正在更改known_faces变量, recognize function 也无法识别bob ,即它正在使用的变量 ( known_faces ) 不会自动更新。

I want to do something like whenever any person uploads any image from the frontend, its encodings should be updated in the file ( camera.py ) without the camera being stopped ie recognize function should not stop its execution.我想做一些事情,比如每当有人从前端上传任何图像时,它的编码应该在文件( camera.py )中更新而不停止相机,即recognize function 不应停止其执行。

PS. PS。 NOTE: Both the files, ie camera.py and check.py are running all the time in 2 different shells.注意:这两个文件,即camera.pycheck.py一直在 2 个不同的 shell 中运行。

Please tell me if it is possible, or is there any other approach to achieve this.请告诉我是否有可能,或者是否有任何其他方法可以实现这一目标。

The absolutely simplest fix for your scenario is probably to have check.py monitor the file and restart the other process when necessary.最简单的解决方案可能是让check.py监视文件并在必要时重新启动其他进程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM