栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python+zmq实现负载均衡

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python+zmq实现负载均衡

# encoding: utf-8

import zmq
import time
import random
from threading import Thread
from vbsl.config import *
from vbsl.get_logs import *
from vbsl.parse_logs import *

NBR_WORKERS = 30


def worker_thread(context=None):
    context = context or zmq.Context.instance()
    worker = context.socket(zmq.REQ)
    worker.connect("tcp://localhost:5671")

    total = 0
    while True:
        # Tell the router we're ready for work
        worker.send(b"ready")

        # Get workload from router, until finished
        workload = worker.recv_string()
        finished = workload == "END"
        if finished:
            print("Processed: %d tasks" % total)
            break
        total += 1

        # Do the work
        station = workload
        ip = get_ip(station)
        if ip is None:
            continue
        print(station, ip)
        file_name = f"/tmp/{station}-{get_date()}.log"
        if os.path.isfile(file_name):
            print(f"{file_name} exist.")
        else:
            print("start script")
            os.system(F"ssh jumper "/home/bsp/IP/get_logs.sh {ip} {station} {get_date()}"")


def main():
    context = zmq.Context.instance()
    client = context.socket(zmq.ROUTER)
    client.bind("tcp://*:5671")

    for _ in range(NBR_WORKERS):
        Thread(target=worker_thread).start()

    random.shuffle(STATIONS)
    print(len(STATIONS))
    print(STATIONS)

    for station in STATIONS:
        # LRU worker is next waiting in the queue
        address, empty, ready = client.recv_multipart()
        client.send_multipart([
            address,
            b'',
            bytes(station, encoding="utf-8"),
        ])
        time.sleep(1)

    # Now ask mama to shut down and report their results
    for _ in range(NBR_WORKERS):
        address, empty, ready = client.recv_multipart()
        client.send_multipart([
            address,
            b'',
            b'END',
        ])


if __name__ == "__main__":
    main()

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/315749.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号