利用python在linux下分布式任务管理

来源:互联网 发布:js获取flash对象 编辑:程序博客网 时间:2024/05/19 07:26

本人是新手,还请各位大神指教,觉得可以的话记得点赞!!!

我们先建一个python文件

import random, time, queue,uuid

from multiprocessing.managers import BaseManager
import threading
import pymysql
def settask(n):
    connect = pymysql.Connect(  
            host='填自己外网地址',  
            port=3306,  
            user='用户名',  
            passwd='密码',  
            db='数据库名',  
            charset='utf8'  
            )  

    cursor = connect.cursor()
    uid = str(uuid.uuid1())
    sql = "INSERT INTO task (rq,name,pc,canshu,uid) VALUES ('%s','%s','%s','%s','%s')" #task是建在数据库的表名
    rq =  time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) 
    name ='pc1name'
    pc = 'pc1'
    canshu = str(n)
    data = (rq,name,pc,canshu,uid)
    print(sql%data)
    
    cursor.execute(sql%data)  
    connect.commit()
    connect.commit() 
    cursor.close()  
    connect.close()
def readtask():
    connect = pymysql.Connect(  
            host='外网地址',  
            port=3306,  
            user='用户名',  
            passwd='密码',  
            db='数据库名',  
            charset='utf8'  
            )  
    cursor = connect.cursor()
    sql  ='select * from  task where ip is null order by rq desc '
    print(sql)
    cursor.execute(sql)
    row = cursor.fetchone()
    description=cursor.description  #列名
    tbhnames= list(map(lambda x:x[0]  ,description)) #列名 
    tbhtypes= list(map(lambda x:x[1]  ,description))#数据类型
    tbhsizes= list(map(lambda x:x[3]  ,description))#数据定义的长度
    rq=row[tbhnames.index('rq')]
    name=row[tbhnames.index('name')] 
    pc=row[tbhnames.index('pc')]
    canshu=row[tbhnames.index('canshu')]

    
            
    connect.commit() 
    cursor.close()  
    connect.close()
    return(str(row))
def dotask():
    settask(random.random() * 10)
    task.put(readtask())
 
    global timer
    timer = threading.Timer(5.5, dotask)
    timer.start()


# 发送任务的队列:
task_queue = queue.Queue()
# 接收结果的队列:
result_queue = queue.Queue()


# 从BaseManager继承的QueueManager:
class QueueManager(BaseManager):
    pass


# 把两个Queue都注册到网络上, callable参数关联了Queue对象:
QueueManager.register('get_task_queue', callable=lambda: task_queue)
QueueManager.register('get_result_queue', callable=lambda: result_queue)
# 绑定端口5000, 设置验证码'abc':
manager = QueueManager(address=('172.18.83.9', 8100), authkey=b'abc')
# 启动Queue:
manager.start()
# 获得通过网络访问的Queue对象:
task = manager.get_task_queue()
result = manager.get_result_queue()


timer = threading.Timer(1, dotask)

timer.start()

再新建一个客户端的python文件

import time, sys, queue
from multiprocessing.managers import BaseManager
import threading
# 创建类似的QueueManager:
class QueueManager(BaseManager):
    pass


def mktask():
    connect = pymysql.Connect(  
            host='外网地址',  
            port=3306,  
            user='用户名',  
            passwd='密码',  
            db='数据库名',  
            charset='utf8'  
            )  
    cursor = connect.cursor()
    sql  ='update task set ip= from  task where ip is null '
    print(sql)
    cursor.execute(sql)
    row = cursor.fetchone()
    description=cursor.description  #列名
    tbhnames= list(map(lambda x:x[0]  ,description)) #列名 
    tbhtypes= list(map(lambda x:x[1]  ,description))#数据类型
    tbhsizes= list(map(lambda x:x[3]  ,description))#数据定义的长度
    rq=row[tbhnames.index('rq')]
    name=row[tbhnames.index('name')] 
    pc=row[tbhnames.index('pc')]
    canshu=row[tbhnames.index('canshu')]

    
            
    connect.commit() 
    cursor.close()  
    connect.close()
    return(str(row))
def gettask():
    print('get',task.get(timeout = 1))
    global timer
    timer = threading.Timer(3, gettask)
    timer.start()


try:
# 由于这个QueueManager只从网络上获取Queue,所以注册时只提供名字:
    QueueManager.register('get_task_queue')
    QueueManager.register('get_result_queue')
    
    # 连接到服务器,也就是运行task_master.py的机器:
    server_addr = '内网地址'
    print('Connect to server %s...' % server_addr)
    # 端口和验证码注意保持与task_master.py设置的完全一致:
    m = QueueManager(address=(server_addr, 8100), authkey=b'abc')
    # 从网络连接:
    m.connect()
    # 获取Queue的对象:
    task = m.get_task_queue()
    result = m.get_result_queue()
    timer = threading.Timer(1, gettask)
    timer.start()
except Exception as e:
    print(e)
finally:
    pass


原创粉丝点击