我有一个python脚本。它正在使用多重处理(Process)。当作为前台进程执行时,它可以正常工作:
watch -n 1 -p python3 ./main.py
但是在后台执行时,会在几个小时后停止工作:
watch -n 1 -p python3 ./main.py &>/dev/null &
OR
watch -n 1 -p python3 ./main.py > /dev/null 2>&1 &
请提供任何可能导致此问题的建议。
编辑:添加了python代码
Python代码:
import sys
import threading
import json
import pandas as pd
import time
import datetime
from datetime import timedelta, timezone
from multiprocessing import Process
influx_db = InfluxDb(database_config["host"], database_config["port"], database_config["user"], database_config["password"], database_config["db_name"])
aws_iot = AwsIoT()
def process_rig(rig):
try:
start_time = time.time()
postgres_db = PostgresDb(pg_connection_string)
XXX(rig, postgres_db)
postgres_db.close()
print(f"{datetime.datetime.now().time()} {rig} - completed processing. seconds: {time.time() - start_time}")
except Exception as e:
print(e.message, e.args)
def XXX(rig, postgres_db):
# some code. removed
t1 = threading.Thread(target=insert_result_in_db, args=(dys_flag, from_date, report_add_result, rig, to_date, utc_now, postgres_db))
t1.start()
t2 = threading.Thread(target=publish, args=(dys_flag, from_date_timestamp, report_add_result, rig, to_date_timestamp, utc_now))
t2.start()
t1.join()
t2.join()
def insert_result_in_db(dys_flag, from_date, report_add_result, rig, to_date, utc_now, postgres_db):
postgres_db.insert_result(rig, from_date, to_date, dys_flag, report_add_result[0], report_add_result[1], utc_now)
def publish(dys_flag, from_date_timestamp, report_add_result, rig, to_date_timestamp, utc_now):
# some code. removed
aws_iot.publish(topic_name, payload)
def run(rig_list):
processes = []
for rig in rig_list:
p = Process(target=process_rig, args=(rig, ))
p.start()
processes.append(p)
for process in processes:
process.join()
if __name__ == '__main__':
run(rigs)
使用psycopg2的PostgresDb类:
class PostgresDb:
def __init__(self, connection_string):
self.postgres_pool = psycopg2.pool.ThreadedConnectionPool(1, 3, connection_string)
def insert_x_result(self, rig, start_range, end_range, dys_flag, dysfunction_index, dysfunction_counts, now):
conn = self.postgres_pool.getconn()
cursor = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
cursor.execute("INSERT INTO xxx (rig, start_range, end_range, dys_flag, dysfunction_index, dysfunction_counts, created_on) "
"VALUES (%s, %s, %s, %s, %s, %s, %s)", (rig, start_range, end_range, dys_flag, dysfunction_index, dysfunction_counts, now))
conn.commit()
self.postgres_pool.putconn(conn)
def get_x_result_range(self, rig, start_range, end_range):
conn = self.postgres_pool.getconn()
cursor = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
cursor.execute("select x from xxx where rig = '%s' and start_range >= %s and start_range <= %s", (rig, start_range, end_range))
# print(self.cursor.query)
result = cursor.fetchall()
self.postgres_pool.putconn(conn)
return result
def dx_get_last_record(self, rig):
conn = self.postgres_pool.getconn()
cursor = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
cursor.execute("select x from xx where rig = '%s' order by end_range desc limit 1", (rig, ))
# print(self.cursor.query)
result = cursor.fetchall()
self.postgres_pool.putconn(conn)
return result
def close(self):
self.postgres_pool.closeall()