AK9*_*K91 8 python sql postgresql python-3.x
我的代码卡在clean_up()方法上MyClass()
my_class.py:
import os
import pandas as pd
import psycopg2, pymysql, pyodbc
from db_credentials_dict import db_credentials
class MyClass():
def __init__(self, from_database, to_table_name, report_name):
...
def get_sql(self):
...
def get_connection(self):
...
def query_to_csv(self):
...
def csv_to_postgres(self):
...
def extract_and_load(self):
self.query_to_csv()
self.csv_to_postgres()
def get_final_sql(self):
...
def postgres_to_csv(self):
...
def clean_up(self):
print('\nTruncating {}...'.format(self.to_table_name), end='')
with self.postgres_connection.cursor() as cursor:
cursor.execute("SELECT NOT EXISTS (SELECT 1 FROM %s)" % self.to_table_name)
empty = cursor.fetchone()[0]
if not empty:
cursor.execute("TRUNCATE TABLE %s" % self.to_table_name)
self.postgres_connection.commit()
print('DONE')
print('Removing {}...'.format(self.filename), end='')
if os.path.exists(self.filepath):
os.remove(self.filepath)
print('DONE')
else:
print('{} does not exist'.format(self.filename))
Run Code Online (Sandbox Code Playgroud)
主要.py:
from my_class import MyClass
from time import sleep
bookings = MyClass(from_database='...',to_table_name='...',report_name='...')
bookings2 = MyClass(from_database='...',to_table_name='...',report_name='...')
channel = MyClass(from_database='...',to_table_name='...',report_name='...')
cost = MyClass(from_database='...',to_table_name='...',report_name='...')
tables = [bookings, bookings2, channel, cost]
for table in tables:
table.extract_and_load()
daily_report = MyClass(from_database='...',to_table_name='...',report_name='...')
daily_report.postgres_to_csv()
sleep(10)
for table in tables:
table.clean_up()
Run Code Online (Sandbox Code Playgroud)
当我运行main.py它时,它会运行所有内容,直到最后一个循环即for table in tables: table.clean_up(). 它只是卡在那里,没有错误或警告。
当它自己运行该方法时,它工作正常,即它会截断 postgres 表。需要帮助使其工作并理解为什么在执行所有其他方法时最终方法不执行。
单独运行时的输出clean_up():
Truncating bookings...DONE
Removing bookings.csv...DONE
Truncating bookings2...DONE
Removing bookings2.csv...DONE
Truncating channel...DONE
Removing channel.csv...DONE
Truncating cost...DONE
Removing cost.csv...DONE
Run Code Online (Sandbox Code Playgroud)
整体代码的作用:
你可能认为这是一种疯狂,我同意。我目前正在使用我所拥有的东西并且无法在我的计算机上存储数据,因为它是公司数据(因此截断和删除数据)。
TRUNCATEPostgres 中的子句需要ACCESS EXCLUSIVE锁定关系,也可能触发一些BEFORE TRUNCATE触发器。
我的猜测是问题出在 Postgres 方面,例如TRUNCATE尝试获取ACCESS EXCLUSIVE关系的锁,但它已经被其他人锁定。
首先,检查您的 Postgres 日志。
接下来,检查您的 Postgres 后端在您挂起期间正在做什么TRUNCATE:
SELECT * FROM pg_stat_activity;
Run Code Online (Sandbox Code Playgroud)
接下来,调查锁:
-- View with readable locks info and filtered out locks on system tables
CREATE VIEW active_locks AS
SELECT clock_timestamp(), pg_class.relname, pg_locks.locktype, pg_locks.database,
pg_locks.relation, pg_locks.page, pg_locks.tuple, pg_locks.virtualtransaction,
pg_locks.pid, pg_locks.mode, pg_locks.granted
FROM pg_locks JOIN pg_class ON pg_locks.relation = pg_class.oid
WHERE relname !~ '^pg_' and relname <> 'active_locks';
-- Now when we want to see locks just type
SELECT * FROM active_locks;
Run Code Online (Sandbox Code Playgroud)