[Python] When a task is marked as completed, it can be reacquired

Task distribution and processing is a common problem in many applications. For example, a task could be to process data, compute results, or perform any operation that requires distributed processing. This article will show how to use Python and Flask to create a simple task distribution system, in which the server provides tasks, the client requests to obtain the tasks and marks the tasks as received. After the client completes the tasks, it can be marked as completed and can be restarted. Get tasks.

Service-Terminal

First, we will create a server side using the Flask framework to serve the tasks. We will also use a SQLite database to store tasks.

from flask import Flask, request, jsonify
import threading
import sqlite3

app = Flask(__name__)

# 数据库文件名
DB_FILE = 'tasks.db'

# 初始化数据库表并插入示例任务数据
def initialize_database():
    conn = sqlite3.connect(DB_FILE)
    cursor = conn.cursor()
    cursor.execute('''CREATE TABLE IF NOT EXISTS tasks
                  (id INTEGER PRIMARY KEY AUTOINCREMENT, content TEXT, status INTEGER)''')

    # 插入示例任务数据
    cursor.execute("INSERT INTO tasks (content, status) VALUES (?, ?)", ("Task1", 0))
    cursor.execute("INSERT INTO tasks (content, status) VALUES (?, ?)", ("Task2", 0))
    cursor.execute("INSERT INTO tasks (content, status) VALUES (?, ?)", ("Task3", 0))
    cursor.execute("INSERT INTO tasks (content, status) VALUES (?, ?)", ("Task4", 0))

    conn.commit()
    conn.close()

# initialize_database()

tasks_lock = threading.Lock()

@app.route('/get_task', methods=['GET'])
def get_task():
    task = None
    with tasks_lock:
        conn = sqlite3.connect(DB_FILE)
        cursor = conn.cursor()
        cursor.execute('SELECT id, content FROM tasks WHERE status=0 LIMIT 1')
        row = cursor.fetchone()
        if row:
            task_id, task_content = row
            cursor.execute('UPDATE tasks SET status=1 WHERE id=?', (task_id,))
            conn.commit()
            task = task_content
        conn.close()

    if task:
        return jsonify({'task': task})
    else:
        return jsonify({'task': None})

@app.route('/complete_task', methods=['POST'])
def complete_task():
    task = request.json.get('task')
    if task:
        with tasks_lock:
            conn = sqlite3.connect(DB_FILE)
            cursor = conn.cursor()
            cursor.execute('UPDATE tasks SET status=0 WHERE content=?', (task,))
            conn.commit()
            conn.close()
        return jsonify({'message': f'Task {task} marked as completed'})
    else:
        return jsonify({'message': 'Invalid request'})

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=8888)

In the server-side code, we used Flask to create two routes /get_task and /complete_task for getting tasks and marking task completion. We also initialized a SQLite database and inserted the sample tasks into the database.

client

Now we'll create a simple client that requests a task and marks it as claimed, then completes the task and marks it as completed.

import requests
import threading
import time

# 服务器地址
SERVER_URL = 'http://127.0.0.1:8888'

# 最大并发线程数
MAX_CONCURRENT_THREADS = 5

tasks_lock = threading.Lock()

def get_task():
    response = requests.get(f'{SERVER_URL}/get_task')
    if response.status_code == 200:
        data = response.json()
        return data.get('task')
    else:
        return None

def complete_task(task):
    if task:
        response = requests.post(f'{SERVER_URL}/complete_task', json={'task': task})
        if response.status_code == 200:
            data = response.json()
            print(data.get('message'))

def worker():
    while True:
        task = get_task()
        if task:
            print(f'Received task: {task}')
            # 模拟客户端完成任务的处理
            time.sleep(5)
            # 在实际应用中,这里应该执行任务的实际逻辑
            print(f'Completed task: {task}')
            complete_task(task)
        else:
            print('No tasks available. Waiting for tasks...')
            time.sleep(5)

def main():
    threads = []
    for _ in range(MAX_CONCURRENT_THREADS):
        thread = threading.Thread(target=worker)
        thread.start()
        threads.append(thread)

    for thread in threads:
        thread.join()

if __name__ == "__main__":
    main()

In the client code, we use the requests library to send an HTTP GET request to get the task, and then send an HTTP POST request to complete the task. We also created multiple threads to simulate multiple clients processing tasks at the same time.

This simple task distribution system allows a client to request a task and mark it as claimed, and then complete the task and mark it as completed. Tasks can be reacquired, thus implementing a basic task distribution and processing system.

Guess you like

Origin blog.csdn.net/linjiuxiansheng/article/details/133098432