寻求每30秒将TXT文件接入数据库并同步刷新的解决方案
Got it, let’s walk through a robust, production-ready solution to meet your needs—connecting a TXT file to a database, with a 30-second refresh cycle that inserts and persists the file’s content each time. Here’s how to build this step by step:
The workflow boils down to three repeating steps, triggered every 30 seconds:
- Read the latest content from the TXT file
- Establish a secure connection to your database
- Insert the content into the database (with safeguards to avoid issues like duplicates or failed writes)
We’ll use Python for this example—it’s lightweight, has great libraries for scheduling and database interactions, and is easy to adapt for production. For the database, we’ll use MySQL (the logic translates directly to PostgreSQL, SQLite, etc.).
1. Set Up Dependencies
First, install the required packages:
pip install schedule pymysql python-dotenv
schedule: Simple, intuitive library for recurring taskspymysql: MySQL database connectorpython-dotenv: To store sensitive credentials (like DB passwords) securely
2. TXT File Reading Logic
Write a function to safely read the TXT file, handling common errors like missing files or permission issues:
def read_txt_file(file_path: str) -> str | None: try: with open(file_path, 'r', encoding='utf-8') as txt_file: # Strip extra whitespace/newlines for cleaner storage content = txt_file.read().strip() return content except FileNotFoundError: print(f"Error: TXT file at {file_path} not found.") return None except PermissionError: print(f"Error: No read permission for {file_path}.") return None
3. Database Connection & Insertion Logic
Create functions to connect to the database and insert content—always use parameterized queries to prevent SQL injection:
import pymysql from datetime import datetime from dotenv import load_dotenv import os # Load credentials from .env file (never hardcode them!) load_dotenv() def get_db_connection(): try: return pymysql.connect( host=os.getenv("DB_HOST"), user=os.getenv("DB_USER"), password=os.getenv("DB_PASSWORD"), database=os.getenv("DB_NAME"), cursorclass=pymysql.cursors.DictCursor ) except pymysql.MySQLError as e: print(f"Database connection failed: {str(e)}") return None def insert_txt_content(content: str) -> bool: if not content: print("Empty content—skipping insertion.") return False conn = get_db_connection() if not conn: return False try: with conn.cursor() as cursor: # Assume you have a table `txt_records` with: # id (INT AUTO_INCREMENT PRIMARY KEY), content (TEXT), created_at (DATETIME DEFAULT CURRENT_TIMESTAMP) sql = """INSERT INTO txt_records (content) VALUES (%s)""" cursor.execute(sql, (content,)) conn.commit() print(f"Successfully inserted content at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}") return True except pymysql.MySQLError as e: print(f"Insert failed: {str(e)}") conn.rollback() return False finally: # Always close the connection to avoid resource leaks if conn: conn.close()
4. Schedule the 30-Second Task
Use the schedule library to run the refresh/insert cycle every 30 seconds:
import schedule import time def run_refresh_cycle(): print(f"\nStarting refresh cycle at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}") txt_content = read_txt_file("/path/to/your/file.txt") insert_txt_content(txt_content) # Schedule the task to repeat every 30 seconds schedule.every(30).seconds.do(run_refresh_cycle) # Start the scheduler loop if __name__ == "__main__": print("Scheduler started. Press Ctrl+C to stop.") while True: schedule.run_pending() time.sleep(1)
Don’t skip these—they’ll prevent headaches in a live environment:
- Idempotency: Avoid inserting duplicate content if the TXT file hasn’t changed. Add a
content_hashcolumn (store MD5/SHA-256 of the content) and check for existing hashes before inserting. - Logging: Replace
print()with Python’s built-inloggingmodule to write logs to a file (instead of the console) for debugging. - Retry Logic: Use a library like
tenacityto retry failed database connections or insertions (e.g., if the DB is temporarily down). - Connection Pooling: For high-traffic scenarios, use a connection pool (like
DBUtils) instead of creating a new connection every 30 seconds. - System-Level Scheduling: If you don’t want to run a Python script 24/7, use Linux
cron(note: cron’s minimum interval is 1 minute, so adjust if 30-second precision is non-negotiable) or Windows Task Scheduler.
If Python isn’t your jam:
- Java/Spring Boot: Use
@Scheduled(fixedRate = 30000)for scheduling, and Spring Data JPA for database operations. - Node.js: Use
node-schedulefor timing,mysql2for DB connections, andfs.promises.readFile()to read the TXT file.
内容的提问来源于stack exchange,提问作者Ashan Lakmal




