Last active
September 15, 2020 13:46
-
-
Save GandalfLiu/bfa270cbd9dd0297e4c871f5cbb4c7fc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pymysql | |
class TaskServer: | |
def __int__(self): | |
self.client = pymysql.connect("xxxxx") | |
def query_task(self): | |
cursor = self.client.cursor(cursor=pymysql.cursors.DictCursor) | |
sql = "select id from table where status = 0" | |
cursor.execute(sql) | |
res = cursor.fetchall() | |
cursor.close() | |
return res | |
def update_runnig_task(self, task_id): | |
cursor = self.client.cursor(cursor=pymysql.cursors.DictCursor) | |
sql = "update table set status=1 where id = {}".format(task_id) | |
cursor.execute(sql) | |
self.client.commit() | |
cursor.close() | |
def get_task(self, task_id): | |
cursor = self.client.cursor(cursor=pymysql.cursors.DictCursor) | |
sql = "select * from table where id = {}".format(task_id) | |
cursor.execute(sql) | |
res = cursor.fetchone() | |
cursor.close() | |
return res | |
def save_result(self, task, result): | |
cursor = self.client.cursor(cursor=pymysql.cursors.DictCursor) | |
update_task_sql = "update table set status={}".format(task["status"]) | |
insert_result_sql = "xxxxxxxxxx" | |
cursor.execute(update_task_sql) | |
cursor.execute(insert_result_sql) | |
self.client.commit() | |
cursor.close() | |
def close(self): | |
if self.client: | |
self.client.close() | |
self.client = None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from controller import TaskServer | |
class Logic: | |
def __init__(self, id): | |
self.id = id | |
self.taskServer = TaskServer() | |
self.task = self.taskServer.get_task(self.id) | |
def run(self): | |
""" | |
0代表未执行, 1正在执行, 2代表执行失败, 3代表执行成功 | |
:return: | |
""" | |
self.taskServer.update_runnig_task(self.id) | |
self.task["status"] = 1 | |
try: | |
""" | |
res = do task | |
""" | |
res = "function do task" | |
if "成功" in res.get("content"): | |
self.task["status"] = 3 | |
else: | |
self.task["status"] = 2 | |
self.taskServer.save_result(self.task, res) | |
except: | |
self.task["status"] = 2 | |
res = "fuction make error result" | |
self.taskServer.save_result(self.task, res) | |
finally: | |
if self.task["status"] in (0, 1): | |
res = "function make error result" | |
self.task["status"] = 2 | |
self.taskServer.save_result(self.task, res) | |
def __del__(self): | |
if self.taskServer: | |
self.taskServer.close() | |
self.taskServer = None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from controller import TaskServer | |
from threading import Thread | |
from queue imoprt Queue | |
from logic import Logic | |
import time | |
q = Queue(3) | |
def create_task(): | |
global q | |
while 1: | |
db_driver = TaskServer() | |
task = db_driver.query_task() | |
if task: | |
q.put(task[0]) | |
db_driver.close() | |
db_driver = None | |
time.sleep(5) | |
def execute_task(): | |
global q | |
while 1: | |
task = q.get() | |
driver = Logic(task) | |
driver.run() | |
driver = None | |
Thread(target=create_task).start() | |
Thread(target=execute_task).start() | |
Thread(target=execute_task).start() | |
Thread(target=execute_task).start() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment