微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python脚本创建重复的进程

如何解决Python脚本创建重复的进程

我的python3脚本创建了自己的副本。它不应该这样做,我也不知道它是如何(或为什么)这样做的。为什么要创建重复项,以及如何阻止这样做?

两个进程同时运行,并且彼此不干扰。但是,脚本会监视目录并根据添加到该目录的文件发布MQTT消息。最终结果是,MQTT代理收到两条相同的消息,彼此间隔几秒钟,每个进程一条。

该脚本是从命令行调用的。

$ python3 fwedge_camera.py -i local_camera.json -b ignition_broker.json

ps -ef中,看起来只有一个过程:

$ ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
...
devuser  11971 11958  0 14:13 pts/1    00:00:00 /bin/bash
devuser  12046 11971 39 15:17 pts/1    00:01:48 python3 fwedge_camera.py -i local_camera.json -b ignition_broker.json

htop中可见重复的过程:

__PID USER      PRI  NI  VIRT   RES   SHR S cpu% MEM%   TIME+  Command
11971 devuser    20   0  2960  1640  1172 S  0.0  0.2  0:00.12 /bin/bash
12046 devuser    20   0 47564 25636  3500 R 47.7  2.5  2:41.12 `- python3 fwedge_camera.py -i local_camera.json -b ig
12054 devuser    20   0 47564 25636  3500 S  0.0  2.5  0:00.52    `- python3 fwedge_camera.py -i local_camera.json -b
12053 devuser    20   0 47564 25636  3500 R 48.4  2.5  2:32.86    `- python3 fwedge_camera.py -i local_camera.json -b

两个进程之间的资源分配总是这样的;第一个过程消耗了大部分资源。

这是源代码。我省略了250行数据处理和MQTT发布。我将根据要求发布未删节的消息源。

# File fwedge_camera.py
#
# This version of `fwedge_camera.py` performs the following tasks:
# - Watch the directories at:
#     /home/devuser/xxxxx/<date>/<time>/
#   for added files.
# - Extract data from the `prediction.txt` file.
# - Publish the data using MQTT Sparkplug B.
# - Publish the `image.txt` file using MQTT Sparkplug B.

from binascii import b2a_base64
from datetime import datetime
import getopt
import json
import os # for remove,listdir,environ,path.getsize
from re import split
import string
import sys
from time import sleep

import paho.mqtt.client as mqtt
from watchdog.observers.polling import PollingObserver
from watchdog.events import PatternMatchingEventHandler

sys.path.insert(0,"./tahu/client_libraries/python/")
import sparkplug_b as sparkplug
from sparkplug_b import *

from date_to_epoch import date_to_epoch
from mulato import Mulato 
from clibridge import read_setting_value

# [ ... 250 lines deleted ... ]

##### Main part of program #####

def usage():
    print('python parse_chooch.py <options>')
    print('Options:')
    print('-h or --help                           : print this message')
    print('-i <file.json> or --input=<file.json>  : specify input device')
    print('-b <file.json> or --broker=<file.json> : specify MQTT broker')
    print('--debug                                : debug (verbose) output')

def parse_cmdline(argv):
    debug_flag = False
    local_file = ""
    mqtt_file = ""
    # logprint("Args are:",argv)
    try:
        opts,args = getopt.getopt(argv,"hi:b:",["help","input-device=","broker=","debug"])
    except getopt.GetoptError:
        logprint("Error: Command line Syntax error")
        print("Error: Command line Syntax error")
        usage()
        sys.exit(2)
    for opt,arg in opts:
        if opt in ("-h","--help"):
            usage()
            sys.exit()
        elif opt in ("-i","--input"):
            if os.path.isfile(arg):
                local_file = arg
            else:
                logprint("Error: input file '%s' not found" % arg)
                print("Error: input file '%s' not found" % arg)
                usage()
                sys.exit(2)
        elif opt in ("-b","--broker"):
            if os.path.isfile(arg):
                mqtt_file = arg
            else:
                logprint("Error: broker file '%s' not found" % arg)
                print("Error: broker file '%s' not found" % arg)
                usage()
                sys.exit(2)
        elif opt in ("--debug"):
            debug_flag = True
    if local_file == "" or mqtt_file == "":
        logprint("Error: must specify an input file and a broker file")
        print("Error: must specify an input file and a broker file")
        usage()
        sys.exit(2)
    return local_file,mqtt_file,debug_flag

def on_modified(event):
    # Watchdog function.
    #
    # Make sure the directory has a file "prediction.txt" in it
    # If it does,return the full pathname and the directory name
    # Otherwise,we don't care
    if event.is_directory:
        pathname = event.src_path
        logprint("Directory",pathname,"modified")
        if os.path.isfile(pathname + '/prediction.txt'):
            pathname = event.src_path
            dirname = pathname.split('/')[-1]
            do_stuff([pathname])

def on_created(event):
    # Watchdog function.
    #
    # Make sure the directory has a file "prediction.txt" in it
    # If it does,"created")
        if os.path.isfile(pathname + '/prediction.txt'):
            pathname = event.src_path
            dirname = pathname.split('/')[-1]
            do_stuff([pathname])

def do_stuff(directories):
    global datapoints
    for d in directories:
        event_id = d.split("/")[-1].replace("-","")
        # logprint(d," ",event_id)
        json_data,count,score = read_json_file(d + '/prediction.txt')
        # Only publish events above a minimum score threshold.
        if score > score_THRESHOLD:
            the_stats_JSON_object = parse_json(json_data,event_id,count) 
            try:
                image_size,imagebytes,image_string = get_image(d + '/image.jpg')
            except:
                image_na = './image_not_available.jpg'
                logprint("File %s not found. Using %s instead." % \
                        (d + '/image.jpg',image_na))
                image_size,image_string = get_image(image_na)
            datapoints = datapoints[:-1]
            megastring = create_megastring(the_stats_JSON_object,image_string)
            # print("MEGASTRING: ",megastring)
            publishDData(mqttClient);
    logprint("*****");

def do_nothing_forever():
    while(True):
        sleep(3600)

if __name__ == '__main__':
    local_file,debug_flag = parse_cmdline(sys.argv[1:])
    logprint("\n=====\nStarted %s on %s" % (sys.argv[0],datetime.Now()))
    mq_groupid,mq_device = setup_local(local_file)
    mq_nodename = set_nodename()
    mqttClient = setup_mqtt(mqtt_file)
    if mqttClient == None:
        do_nothing_forever()
    interval = set_interval()
    homedir = os.environ['HOME']
    logpath = homedir + '/apps/xxxxx'
    patterns = "20*"
    ignore_patterns = ""
    ignore_directories = False
    case_sensitive = True
    my_event_handler = PatternMatchingEventHandler(
            patterns,ignore_patterns,ignore_directories,case_sensitive)
    my_event_handler.on_created = on_created
    my_event_handler.on_modified = on_modified
    observer = PollingObserver()
    logprint("Scheduled observer for path " + logpath + \
            " and pattern '" + patterns + "'")
    o = observer.schedule(my_event_handler,logpath,recursive=True)
    # logprint("Observer is watching " + o.path + " and recursive is " + o.recursive)
    logprint("Observer is watching " + o.path)
    observer.start()
    try:
        while(True):
            sleep(1)
            mqttClient.loop()
    except KeyboardInterrupt:
        logprint("Stopped",sys.argv[0],"gracefully")
        observer.stop()
    observer.join()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。