利用python在linux下分布式任务管理
来源:互联网 发布:js获取flash对象 编辑:程序博客网 时间:2024/05/19 07:26
本人是新手,还请各位大神指教,觉得可以的话记得点赞!!!
我们先建一个python文件
import random, time, queue,uuid
from multiprocessing.managers import BaseManagerimport 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
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
阅读全文
0 0
- 利用python在linux下分布式任务管理
- ubuntu下 python分布式任务管理
- linux下任务管理
- 在Windows下利用Python控制Linux
- 教你在linux下利用python充当计算器
- 利用Linux中的crontab实现分布式项目定时任务
- Linux下利用crontab执行任务
- linux下利用crontab创建定时任务
- Linux 下利用corn添加定时任务
- Python在windows下的分布式进程
- python 利用Shelve实现在命令行下的数据自动管理
- Linux下Python版本管理
- 在Linux下搭建Hadoop分布式集群
- 在 Linux 下使用任务管理器
- linux下利用crontab来实现定时任务
- Linux下怎么来利用crontab实现计划任务
- 利用桌面背景进行任务管理
- 在Ubuntu 11.04环境下管理联系人、日历和任务
- Spring boot 搭建个人博客系统(五)——标签和标签云
- 【拜小白opencv】19-保存视频操作——写操作
- 例2-22 正弦信号的微分运算和积分运算
- 一句话反弹shell
- 2017年8月21日训练日记
- 利用python在linux下分布式任务管理
- 8.21
- xlistView下拉刷新,上拉加载更多
- 无序容器
- 归并排序(algs4)
- 2017.8.21-------树状数组
- DOM
- 堆
- Retrofit+fastjson