1 Star 0 Fork 0

lunny/LPan

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
taskmgr.py 1.70 KB
一键复制 编辑 原始数据 按行查看 历史
lunny 提交于 2012-12-21 09:38 . updated
#! /usr/bin/env python
#coding=utf-8
from threading import Thread, Lock
tasks = {}
count = 0
pennding, running, pause, done, error = range(5)
l = Lock()
max_thread = 3
def add_task(type, params):
global tasks, l, count
l.acquire()
count = count + 1
id = count
tasks[id] = [(type, params), pennding]
l.release()
return id
def cacel_task(taskid):
global tasks, l
l.acquire()
del tasks[taskid]
l.release()
threads = {}
def downing(client, task):
params = task[0][1]
try:
data = client.download(params['path'])
f = open(params['localpath'], 'wb')
f.write(data)
f.close()
task[1] = done
#db[path_key] = file['sha1']
except:
task[1] = error
def uploading(client, task):
params = task[0][1]
try:
client.upload(params['path'], params['localpath'], root="app_folder", overwrite=params['overwrite'])
#db[path_key] = file['sha1']
task[1] = done
except:
task[1] = error
def start_task(client, task_id):
task = tasks[task_id]
type = task[0][0]
if type == 'download':
threads[task_id] = Thread(target=downing, args=(client, task))
elif type == 'upload':
threads[task_id] = Thread(target=uploading, args=(client, task))
else:
assert False
tasks[task_id][1] = running
threads[task_id].start()
def stop_task():
pass
def run(client, event):
for id, task in task.iteritems():
if evt.isSet():
break
if len(threads.keys()) < max_thread:
status = task[1]
if status == pennding:
start_task(client, id)
def pause():
pass
def stop():
pass
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/lunny/lpan.git
git@gitee.com:lunny/lpan.git
lunny
lpan
LPan
master

搜索帮助