本文整理汇总了Python中ThreadPool.ThreadPool.start方法的典型用法代码示例。如果您正苦于以下问题:Python ThreadPool.start方法的具体用法?Python ThreadPool.start怎么用?Python ThreadPool.start使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ThreadPool.ThreadPool
的用法示例。
在下文中一共展示了ThreadPool.start方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_server_list
# 需要导入模块: from ThreadPool import ThreadPool [as 别名]
# 或者: from ThreadPool.ThreadPool import start [as 别名]
def test_server_list(cls, target_list, shared_settings):
"""
Tests connectivity with each server of the target_list and returns
the list of online servers.
"""
# Use a thread pool to connect to each server
thread_pool = ThreadPool()
for target_str in target_list:
thread_pool.add_job((cls._test_server, (target_str, shared_settings)))
nb_threads = min(len(target_list), cls.MAX_THREADS)
thread_pool.start(nb_threads)
# Return valid targets
for (job, target) in thread_pool.get_result():
yield target
# Use None as a sentinel
yield None
# Return invalid targets
for (job, exception) in thread_pool.get_error():
yield exception
thread_pool.join()
return
示例2: test_connectivity
# 需要导入模块: from ThreadPool import ThreadPool [as 别名]
# 或者: from ThreadPool.ThreadPool import start [as 别名]
def test_connectivity(self, timeout):
"""
Tests connectivity with each server of the target_list and returns
the list of online servers.
"""
# Use a thread pool to connect to each server
thread_pool = ThreadPool()
for target_str in self._target_list:
thread_pool.add_job((self._test_server,
(target_str, timeout)))
nb_threads = min(len(self._target_list), self.MAX_THREADS)
thread_pool.start(nb_threads)
# Recover valid targets
for (job, target) in thread_pool.get_result():
self._targets_OK.append(target)
yield target
# Store invvalid targets
for (job, exception) in thread_pool.get_error():
self._targets_ERR.append(exception)
thread_pool.join()
return
示例3: handler
# 需要导入模块: from ThreadPool import ThreadPool [as 别名]
# 或者: from ThreadPool.ThreadPool import start [as 别名]
def handler( ips_cmds ):
taskCount = len( ips_cmds )
if taskCount > _THREAD_NUMBER:
threadCount = _THREAD_NUMBER
else:
threadCount = taskCount
pool=ThreadPool(threadCount)
for ip, cmds in ips_cmds:
pool.addTask(telnetTask, ip = ip, cmds = cmds )
pool.start()
dlg = wx.ProgressDialog("Waitng", "Waiting",
maximum = taskCount,
parent= None,
#style = wx.PD_CAN_ABORT|wx.PD_APP_MODAL
)
while pool.getFinishCount() < taskCount:
dlg.Update(pool.getFinishCount(), "%d of %d" %(pool.getFinishCount(), taskCount))
wx.MilliSleep(100)
dlg.Destroy()
result = pool.show()
pool.join()#pool itself is also a thread
return result
示例4: OnExecute
# 需要导入模块: from ThreadPool import ThreadPool [as 别名]
# 或者: from ThreadPool.ThreadPool import start [as 别名]
def OnExecute(self, button_desc):
if "run_detect_button" == button_desc:
desc_fail = "运行检查程序返回失败\r\n"
desc_success = "所有设备运行检查程序返回成功\r\n"
cmds_txt = "./cmds/cmds_detect.txt"
if "scan_result_button" == button_desc:
desc_fail = "查看检查结果返回失败\r\n"
desc_processexit = "设备检测程序异常退出\r\n"
desc_success = "所有设备查看检查结果返回成功\r\n"
cmds_txt = "./cmds/cmds_scan.txt"
ips = self.ParseIp(self.ip.GetValue().encode('utf-8').split(';'))
if False == ips:
return
cmds = getContent(cmds_txt,"lines")
if False == cmds:
return
pool = ThreadPool( _THREAD_POOL_SIZE )
for ip in ips:
pool.addTask( telnetTask, ip = ip, cmds = cmds)
#必须放在pool.addFinishCb( finish_event,pool=pool)之前
def finish_event( *args, **kwargs ):
pool = kwargs['pool']
result = pool.show()
# [[ip,[ret1,ret2]],...]
result_to_show = ""
for ip_retlist in result:
ip = ip_retlist[0]
retlist = ip_retlist[1]
if ["fail"] == retlist:
result_to_show += ip + " Telnet时抛出了异常\r\n"
else:
t = retlist[len(retlist)-1].split("\r\n")
status = t[len(t)-2]
if "0" == status:
pass
elif "1" == status:
result_to_show += ip + desc_fail#desc_fail能访问到
elif "2" == status:
result_to_show += ip + desc_processexit
else:
result_to_show += ip + " 脚本返回意料之外的值\r\n"
last_cmd_result = retlist[len(retlist)-1]
logging.warning(toUnicode(last_cmd_result))
if "" == result_to_show:
result_to_show = desc_success
logging.warning(result_to_show.decode('utf-8'))
result_to_show = self.result_text.GetValue()+getCurTime().decode("ascii")+" ".decode("utf-8")+result_to_show.decode("utf-8")
self.result_text.SetValue(result_to_show)
self.ButtonStatus("Enable")
pool.addFinishCb( finish_event,pool=pool)
pool.start()
self.ButtonStatus("Disable")
示例5: DownloadServer
# 需要导入模块: from ThreadPool import ThreadPool [as 别名]
# 或者: from ThreadPool.ThreadPool import start [as 别名]
class DownloadServer():
def __init__(self, is_debug=False):
self.prev_day = {}
self.thread_pool = ThreadPool(
is_debug,
int(cfg.read('max_threads')),
int(cfg.read('max_buf')),
)
# 注意先清空先前留下的下载任务
# TODO: 改为继续执行先前未完成的任务
db.Execute("DELETE FROM `CurrentTask`")
def start(self):
# 首先启动线程池所有线程
self.thread_pool.start()
# 然后启动日历线程更新任务列表
start_new_thread(self.calendar_daemon, ())
# 同时启动生产者线程
start_new_thread(self.worker_daemon, ())
# 最后启动配额管理线程
start_new_thread(self.cleaner_daemon, ())
# 这个方法对于每个任务,判断是否进入了新的一天
# 如果是的话,就将新的任务增加到任务列表中
def update_calendar(self, overwrite_time=None):
# 注意同一个规则所实例化的任务,只能被添加一次
sql = "SELECT * FROM `UserTask` WHERE " \
"`TaskID` NOT IN (SELECT `TaskID` FROM `CurrentTask`) " \
"AND `Status` != 0"
this_day = {}
all_tasks = db.Query(sql)
for task in all_tasks:
# 首先读取时区信息,并转换为当前任务所在时区的时间
TimeZone = timezone(str(task[6]))
if overwrite_time is None:
today = datetime.now(TimeZone)
else:
today = overwrite_time
# 然后判断在该时区是否进入了新的一天
is_new_day = False
if self.prev_day.get(TimeZone, None) is None \
or today.day != self.prev_day[TimeZone]:
this_day[TimeZone] = today.day
is_new_day = True
# 如果确实进入了新的一天
if is_new_day:
# 首先生成任务开始和结束时间
# 同样注意转换为任务所在的时区
date_nums = map(int, task[5].split())
StartTime = datetime(year=today.year,
month=today.month,
day=today.day,
hour=date_nums[3],
minute=date_nums[4],
tzinfo=TimeZone)
FinishTime = StartTime + timedelta(hours=task[10])
# 生成一些与日期相关的数据
yesterday = today + timedelta(days=-1)
tomorrow = today + timedelta(days=1)
keywords = {
'%year%': today.year,
'%mon%': today.month,
'%day%': today.day,
'%prev_year%': yesterday.year,
'%prev_mon%': yesterday.month,
'%prev_day%': yesterday.day,
'%next_year%': tomorrow.year,
'%next_mon%': tomorrow.month,
'%next_day%': tomorrow.day
}
for key in keywords.keys():
keywords[key] = '%02d' % keywords[key]
# 其次生成下载链接
# 用dict中的关键字不断替换URL中字符串
TaskID = task[0]
UID = task[1]
URL = task[2]
for key in keywords.keys():
while URL.find(key) != -1:
URL = URL.replace(key, keywords[key])
# 生成URL后,更新文件保存位置:
# 1. 首先读取全局位置
Location = cfg.read('global_pos')
# 2. 然后定位到用户家目录位置
Location = os.path.join(Location, UID)
# 3. 再定位到规则目录位置,这里的类型都是unicode
RuleName = task[3]
SubDir = task[8]
if SubDir:
if type(SubDir) == str:
SubDir = SubDir.decode('utf-8')
Location = os.path.join(Location, SubDir)
# 4. 最后根据命名规则确定文件名
if task[9] == 'auto':
Location = os.path.join(Location, URL.split('/')[-1])
else:
#.........这里部分代码省略.........
示例6: OnPatch
# 需要导入模块: from ThreadPool import ThreadPool [as 别名]
# 或者: from ThreadPool.ThreadPool import start [as 别名]
def OnPatch(self,evt):
desc_ddr2 = "DDR2不需要修补补丁\r\n"
desc_fail = " 修补补丁失败\r\n"
desc_md5 = "补丁Md5值校验失败\r\n"
desc_notexist = "补丁文件不存在\r\n"
desc_success = "所有设备打补丁成功,请断电\r\n"
cmds_txt = "./cmds/cmds_patch.txt"
ips = self.ParseIp(self.ip.GetValue().encode('utf-8').split(';'))
if False == ips:
return
cmds = getContent(cmds_txt,"lines")
if False == cmds:
return
pool = ThreadPool( _THREAD_POOL_SIZE )
for ip in ips:
pool.addTask( telnetTask, ip = ip, cmds = cmds)
#必须放在pool.addFinishCb( finish_event,pool=pool)之前
def finish_event( *args, **kwargs ):
pool = kwargs['pool']
result = pool.show()
# [[ip,[ret1,ret2]],...]
result_to_show = ""
for ip_retlist in result:
ip = ip_retlist[0]
retlist = ip_retlist[1]
if ["fail"] == retlist:
result_to_show += ip + " Telnet时抛出了异常\r\n"
else:
t = retlist[len(retlist)-1].split("\r\n")
status = t[len(t)-2]
if "0" == status:
pass
elif "1" == status:
result_to_show += ip + desc_ddr2
elif "2" == status:
result_to_show += ip + desc_md5
elif "3" == status:
result_to_show += ip + desc_notexist
elif "4" == status:
result_to_show += ip + desc_fail
else:
result_to_show += ip + " 脚本返回意料之外的值\r\n"
last_cmd_result = retlist[len(retlist)-1]
logging.warning(toUnicode(last_cmd_result))
if "" == result_to_show:
result_to_show = desc_success
if "请断电" in desc_success:
wx.CallAfter(pub.sendMessage,"importNote",("请将设备断电"))
logging.warning(result_to_show.decode('utf-8'))
result_to_show = self.result_text.GetValue()+getCurTime().decode("ascii")+" ".decode("utf-8")+result_to_show.decode("utf-8")
self.result_text.SetValue(result_to_show)
self.ButtonStatus("Enable")
pool.addFinishCb( finish_event,pool=pool)
pool.start()
self.ButtonStatus("Disable")