在python中使用任务队列实现服务器

eqoofvh9  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(260)

我正在尝试用任务队列实现http服务器。请参见下面的架构:

我的代码

import http.server
import socketserver
import os
import threading
import time

from queue import Queue

PORT = 8004

def run_parser(thread_id, person_id):
    print(f"{threading.current_thread().name} START {thread_id} {person_id}")
    time.sleep(15)
    print(f"{threading.current_thread().name} STOP {thread_id} {person_id}")

class MyHttpRequestHandler(http.server.SimpleHTTPRequestHandler):
    def __init__(self, *args, directory=None,**kwargs):
        self.person_ids = Queue()
        self.active_threads = []
        self.THREADS_NUM = 10
        self.all_threads = [] # Threading object instances

        super().__init__(*args, directory=None,**kwargs)

    def do_GET(self):
        person_ids = self.path[1:].split(',')
        print("Handle request", person_ids)
        for person_id in person_ids:
            self.process_person(person_id)

    def get_free_thread_id(self):
        if len(self.active_threads) == 0:
            return 0
        for i in range(0, self.THREADS_NUM):
            if i not in self.active_threads:
                return i
        return None # no avaliable threads

    def process_person(self, person_id):
        """Run thread with person parsing task"""
        thread_id = self.get_free_thread_id()
        print(f"process_person: thread_id={thread_id}, person_id={person_id}")
        if thread_id is not None:
            self.active_threads.append(thread_id)
            print(f"{threading.current_thread().name} QQQ_1", self.active_threads)
            cur_thread = threading.Thread(
                name=f"THREAD_{thread_id}",
                target=run_parser, 
                args=((thread_id, person_id)), 
                daemon=False
            )
            cur_thread.start()
            self.all_threads.append(cur_thread)
        else:
            print(f"Put person_id={person_id} to queue")
            self.person_ids.put(person_id)

# Create an object of the above class

handler_object = MyHttpRequestHandler

my_server = socketserver.TCPServer(("", PORT), handler_object)

# Star the server

my_server.serve_forever()

考试题
http://127.0.0.1:8004/qwert2,qwert3
请求1秒后http://127.0.0.1:8004/qwert2,又是qwert3
我有以下输出:

Handle request ['qwert2', 'qwert3']
process_person: thread_id=0, person_id=qwert2
MainThread QQQ_1 [0]
THREAD_0 START 0 qwert2
process_person: thread_id=1, person_id=qwert3
MainThread QQQ_1 [0, 1]
THREAD_1 START 1 qwert3
Handle request ['qwert2', 'qwert3']
process_person: thread_id=0, person_id=qwert2
MainThread QQQ_1 [0]
THREAD_0 START 0 qwert2
process_person: thread_id=1, person_id=qwert3
MainThread QQQ_1 [0, 1]
THREAD_1 START 1 qwert3
THREAD_0 STOP 0 qwert2
THREAD_1 STOP 1 qwert3
THREAD_0 STOP 0 qwert2
THREAD_1 STOP 1 qwert3

似乎每个do_get请求都有自己的队列对象。但我希望所有线程和函数调用都有一个队列
我需要这样的输出(请参阅主线程qq_1[0,1,2,3])如何实现下面的输出?

Handle request ['qwert2', 'qwert3']
    process_person: thread_id=0, person_id=qwert2
    MainThread QQQ_1 [0]
    THREAD_0 START 0 qwert2
    process_person: thread_id=1, person_id=qwert3
    MainThread QQQ_1 [0, 1]
    THREAD_1 START 1 qwert3
    Handle request ['qwert2', 'qwert3']
    process_person: thread_id=0, person_id=qwert2
    MainThread QQQ_1 [0, 1, 2]
    THREAD_0 START 0 qwert2
    process_person: thread_id=1, person_id=qwert3
    MainThread QQQ_1 [0, 1, 2, 3]
...

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题