-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti_thread.py
49 lines (40 loc) · 972 Bytes
/
multi_thread.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# encoding=utf8
"""
多线程(线程池)
"""
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
import time
import threading
import os
import threadpool
class exec_system_command(threading.Thread):
"""
执行系统命令的线程类
"""
def __init__(self, command_str):
threading.Thread.__init__(self)
self.command_str = command_str
def run(self):
os.system(self.command_str)
def exec_system_command(command_str):
"""
执行系统命令
:param command_str:
:return:
"""
print command_str
try:
os.system(command_str)
except Exception as e:
print str(e)
print '[ERROR]something wrong in ' + command_str
# 线程池
pool = threadpool.ThreadPool(100)
thread_exec_commands = []
command = 'xxx'
thread_exec_commands.append(command)
requests = threadpool.makeRequests(exec_system_command, thread_exec_commands)
[pool.putRequest(req) for req in requests]
pool.wait()