本文整理汇总了Python中threadpool.ThreadPool.add_task方法的典型用法代码示例。如果您正苦于以下问题:Python ThreadPool.add_task方法的具体用法?Python ThreadPool.add_task怎么用?Python ThreadPool.add_task使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类threadpool.ThreadPool
的用法示例。
在下文中一共展示了ThreadPool.add_task方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: from_file
# 需要导入模块: from threadpool import ThreadPool [as 别名]
# 或者: from threadpool.ThreadPool import add_task [as 别名]
def from_file(m163,option):
""" download objects (songs, albums...) from an input file. """
urls = []
with open(option.inFile) as f:
urls = f.readlines()
global total, done, xiami_obj
total = len(urls)
print border
LOG.info(msgTxt.fmt_links_in_file % total)
print border
pool = ThreadPool(config.THREAD_POOL_SIZE)
for link in [u for u in urls if u]:
link = link.rstrip('\n')
#if it is a xiami link, init xiami object
if re.match(pat_xm, link):
__init_xiami_obj(option)
pool.add_task(from_url_xm, xiami_obj,link, verbose=False)
elif re.match(pat_163, link):
pool.add_task(from_url_163, m163,link, verbose=False)
else:
LOG.warning(msgTxt.fmt_skip_unknown_url % link)
pool.wait_completion()
示例2: pickle_all_companies
# 需要导入模块: from threadpool import ThreadPool [as 别名]
# 或者: from threadpool.ThreadPool import add_task [as 别名]
def pickle_all_companies():
tpool = ThreadPool(50)
companies = Company.objects.all()
for c in companies:
tpool.add_task(pickle_company, c.symbol)
tpool.wait_completion()
return None
示例3: main
# 需要导入模块: from threadpool import ThreadPool [as 别名]
# 或者: from threadpool.ThreadPool import add_task [as 别名]
def main():
start_time = time.time()
parser = add_parser()
args = parser.parse_args()
app_dir = args.path + "\\" + args.app_dir
global encoding
encoding = args.encoding
#line feed
line_feed = "\n"
# use console to show information
global console
console = Console()
console.show("Target Path: " + args.path)
console.show("Webapp Directory: " + app_dir)
console.show("Testing Website: " + args.website)
console.show("Output File: " + args.output)
# start fetching
console.show("Start fetching url and its parameters in " + args.path)
global url_data
url_data = UrlData()
get_url_list(args.path, app_dir, args.website)
url_amount = url_data.len()
#fetch complete
console.show("Fetched " + str(url_amount) + " url(s).")
if args.get_status != 1 or args.website == "":
url_data.export(args.output)
#exit
sys.exit()
console.show("Start testing url status with " \
+ str(args.thread_num) + " thread(s).")
#init thread pool
pool = ThreadPool(args.thread_num)
for url in url_data.get_urls():
pool.add_task(url_request, url)
console.show_progress(pool.get_progress(), url_amount)
while pool.get_progress() != url_amount:
console.show_progress(pool.get_progress(), url_amount)
#pool.destroy()
finish_time = time.time()
elapsed_time = int(finish_time - start_time)
#export
url_data.export(args.output)
console.show("Task finished in " + str(elapsed_time) + " seconds.")
示例4: prime_cache
# 需要导入模块: from threadpool import ThreadPool [as 别名]
# 或者: from threadpool.ThreadPool import add_task [as 别名]
def prime_cache(self):
"""Ensures that the webpage cache is filled in the
quickest time possible by making many requests in
parallel"""
print "Getting data for parts from suppliers' websites"
pool = ThreadPool(NUM_THREADS)
for srcode, pg in self.iteritems():
print srcode
pool.add_task(pg.get_price)
pool.wait_completion()
示例5: from_file
# 需要导入模块: from threadpool import ThreadPool [as 别名]
# 或者: from threadpool.ThreadPool import add_task [as 别名]
def from_file(xm_obj, infile):
""" download objects (songs, albums...) from an input file. """
urls = []
with open(infile) as f:
urls = f.readlines()
global total, done
total = len(urls)
print border
LOG.info(u' 文件包含链接总数: %d' % total)
print border
pool = ThreadPool(config.THREAD_POOL_SIZE)
for link in [u for u in urls if u]:
pool.add_task(from_url, xm_obj,link.rstrip('\n'), verbose=False)
pool.wait_completion()
示例6: Spider
# 需要导入模块: from threadpool import ThreadPool [as 别名]
# 或者: from threadpool.ThreadPool import add_task [as 别名]
class Spider(object):
def __init__(self,seed,depth,pool_size=10):
self.seed = seed
self.depth = depth
self.all_url_list = [seed]
self.finished_url_list = []
self.failure_url_list = []
self.pool = ThreadPool(pool_size)
def crawl(self):
base_deep_size = 0
while base_deep_size <= self.depth:
for url in self.all_url_list:
if url not in self.finished_url_list:
self.pool.add_task(self.download,url)
self.pool.close()
self.depth-=1
def download(self,url):
try:
data = urllib2.urlopen(url)
page = data.read()
self.finished_url_list.append(url)
links = self.get_urls(page)
return page,links
except Exception as e:
print 'open url:%s raise exception(%s)'%(url,e)
return None
def get_urls(self,page):
soup = BeautifulSoup(page,fromEncoding="gb18030")
if soup.title:
print soup.title.string
links = []
for item in soup.findAll('a'):
link=item.get('href')
if link and link.startswith('http://') and link not in self.finished_url_list:
links.append(link)
print links
return links
def get_next_url(self):
pass
示例7: from_file
# 需要导入模块: from threadpool import ThreadPool [as 别名]
# 或者: from threadpool.ThreadPool import add_task [as 别名]
def from_file(xm_obj,m163, infile):
""" download objects (songs, albums...) from an input file. """
urls = []
with open(infile) as f:
urls = f.readlines()
global total, done
total = len(urls)
print border
LOG.info(u' 文件包含链接总数: %d' % total)
print border
pool = ThreadPool(config.THREAD_POOL_SIZE)
for link in [u for u in urls if u]:
link = link.rstrip('\n')
if re.match(pat_xm, link):
pool.add_task(from_url_xm, xm_obj,link, verbose=False)
elif re.match(pat_163, link):
pool.add_task(from_url_163, m163,link, verbose=False)
else:
LOG.warning(u' 略过不能识别的url [%s].' % link)
pool.wait_completion()
示例8: make_all_pairs
# 需要导入模块: from threadpool import ThreadPool [as 别名]
# 或者: from threadpool.ThreadPool import add_task [as 别名]
def make_all_pairs(use_celery=False, skip_update=False, skip_pickle=False, skip_worker_update=False):
"""
This will check all of the pairs, either threaded
or via celery (i.e. local v cloud)
"""
logger.info(colored('Collecting companies', 'white', attrs=['bold']))
companies = Company.objects.all()
tpool = ThreadPool(50)
if not skip_update:
logger.info(colored('Updating prices', 'white', attrs=['bold']))
for c in companies:
tpool.add_task(c.update_prices)
tpool.wait_completion()
logger.info(colored('Prices updated', 'white', attrs=['bold']))
symbols = [c.symbol for c in companies]
if not skip_pickle:
logger.info(colored('Pickling companies', 'white', attrs=['bold']))
pickle_all_companies()
if not skip_worker_update:
logger.info(colored('Updating workers', 'white', attrs=['bold']))
update_workers()
if use_celery:
for s1, s2 in itertools.combinations(symbols, 2):
make_pair.delay(s1, s2)
else:
for s1, s2 in itertools.combinations(symbols, 2):
tpool.add_task(make_pair, s1, s2)
tpool.wait_completion()
return
示例9: __init__
# 需要导入模块: from threadpool import ThreadPool [as 别名]
# 或者: from threadpool.ThreadPool import add_task [as 别名]
class Database:
"""Asynchronous database interface.
The `driver' argument specifies which database to use. Possible
values are:
MySQLdb - for MySQL
psycopg2 - for Postgres
"""
def __init__(self,
driver=None,
database=None, user=None, password=None,
host='localhost',
ioloop=tornado.ioloop.IOLoop.instance(),
num_threads=10,
tx_connection_pool_size=5,
queue_timeout=1,
thread_idle_life=60*60):
if not(driver):
raise ValueError("Missing 'driver' argument")
self._driver = driver
self._database = database
self._user = user
self._password = password
self._host = host
self._threadpool = ThreadPool(
per_thread_init_func=self.create_connection,
per_thread_close_func=self.close_connection,
num_threads=num_threads,
queue_timeout=queue_timeout,
thread_idle_life=thread_idle_life)
self._ioloop = ioloop
# Connection pool for transactions
self._connection_pool = []
for i in xrange(tx_connection_pool_size):
conn = self.create_connection()
self._connection_pool.append(conn)
self._waiting_on_connection = deque()
def create_connection(self):
"""This method is executed in a worker thread.
Initializes the per-thread state. In this case we create one
database connection per-thread.
"""
# if self._driver == "psycopg2":
# try:
# import psycopg2
# conn = psycopg2.connect(database=self._database,
# user=self._user,
# password=self._password,
# host=self._host)
# except Exception as ex:
# raise ex
if self._driver == "MySQLdb":
try:
import MySQLdb
conn = MySQLdb.connect(db=self._database,
user=self._user,
passwd=self._password,
host=self._host,
port=3306)
except Exception as ex:
raise ex
else:
raise ValueError("Unknown driver %s" % self._driver)
return conn
def close_connection(self, conn):
conn.close()
def stop(self):
self._threadpool.stop()
for conn in self._connection_pool:
conn.close()
@async
def beginTransaction(self, callback):
"""Begins a transaction. Picks up a transaction from the pool
and passes it to the callback. If none is available, adds the
callback to `_waiting_on_connection'.
"""
if self._connection_pool:
conn = self._connection_pool.pop()
callback(conn)
else:
self._waiting_on_connection.append(callback)
@async
def commitTransaction(self, connection, callback):
self._threadpool.add_task(
partial(self._commitTransaction, connection, callback))
def _commitTransaction(self, conn, callback, thread_state=None):
"""Invoked in a worker thread.
"""
conn.commit()
self._ioloop.add_callback(
partial(self._releaseConnectionInvokeCallback, conn, callback))
#.........这里部分代码省略.........
示例10: run
# 需要导入模块: from threadpool import ThreadPool [as 别名]
# 或者: from threadpool.ThreadPool import add_task [as 别名]
def run(self, symnames, init_symbols=None, N=4):
if isinstance(symnames, (str, unicode)):
symnames = [ symnames ]
self.finalize()
toporder = self._topsort_subgraph(symnames)
sources = ( v for v in toporder if len(self.depends[v]) == 0 )
remain_to_submit = SynchronizedCounter(len(toporder))
finished_deps = defaultdict(SynchronizedCounter)
p = ThreadPool(N)
if init_symbols is None:
syms = SymbolTable()
else:
syms = init_symbols
parentlock = RLock()
done_submitting = Condition()
# If the child thread notifies before the parent thread reaches the
# wait statement, then the parent will never receive the notification
# and will block forever. To fix this, the child will decrement this
# counter to zero, and the parent will check this before waiting.
done_submitting_helper = SynchronizedCounter(1)
# The callback runs within the thread. Don't know how to fix.
def make_apply_callback(gf):
def finished(new_syms):
parentlock.acquire()
self.results[gf] = new_syms
parentlock.release()
parentlock.acquire()
# print "%s finished! printing state"%(gf.name)
# print "finished_deps", finished_deps
# print >> sys.stderr, "%s completed. new_syms = %s"%(gf.name, new_syms)
# print "self.depends", self.depends
parentlock.release()
# Update the functions which we precede
for next_gf in self.preceded_by[gf]:
finished_deps_next_gf = finished_deps[next_gf].inc()
if finished_deps_next_gf == len(self.depends[next_gf]):
# All dependencies satisfied; we can run!
# This may take a bit of time, but we want to do
# all data manipulation in this process.
print >> sys.stderr, "Dependencies for %s satisfied. Queueing."%next_gf.name
symtable = SymbolTable(parents=[self.results[r] for r in self.depends[next_gf]])
# Queue doesn't need to be locked
p.add_task(next_gf, args=(symtable,), callback=make_apply_callback(next_gf))
if remain_to_submit.dec() == 0:
print >> sys.stderr, "All jobs have been submitted. Waiting for parent thread to be ready to receive done_submitting"
done_submitting.acquire()
done_submitting.notify()
done_submitting.release()
done_submitting_helper.dec()
return finished
for s in sources:
remain_to_submit.dec()
p.add_task(s, args=(SymbolTable(),), callback=make_apply_callback(s))
if done_submitting_helper.get() > 0:
done_submitting.acquire()
print >> sys.stderr, "PARENT THREAD: Awaiting condition variable"
done_submitting.wait()
done_submitting.release()
print >> sys.stderr, "PARENT THREAD: Joining the thread pool"
p.wait_completion()
ret = dict((sym, self.results[self.supplier[sym]][sym]) for sym in symnames)
return ret
示例11: functional_test
# 需要导入模块: from threadpool import ThreadPool [as 别名]
# 或者: from threadpool.ThreadPool import add_task [as 别名]
def functional_test():
r = redis.StrictRedis(host='localhost', port=8323)
# set/get
print r.set("key", 'b'*56000)
print len(r.get("key"))
# incr
print r.set("incr_key",0)
print r.get("incr_key")
print r.incr('incr_key')
print r.get("incr_key")
def press_test():
r = redis.StrictRedis(host='localhost', port=8323)
for i in range(10000):
key = 'foo_%d'%i
r.set(key, 'b'*i)
if i%1000==0:
print key, "->", len(r.get(key))
if __name__=="__main__":
#functional_test()
# Create thread pool with nums threads
pool = ThreadPool(32)
# Add a task into pool
for n in range(10):
pool.add_task(functional_test)
pool.add_task(press_test)
# Join and destroy all threads
pool.destroy()
示例12: Server
# 需要导入模块: from threadpool import ThreadPool [as 别名]
# 或者: from threadpool.ThreadPool import add_task [as 别名]
#.........这里部分代码省略.........
#NOTE could preallocate the memory for the incomming call, though possible threading problems
lasttime = time.time()
status = MPI.Status()
while True:
assert debug("[" + str(comm.Get_rank()) + "]Listening to remote " + str(MPI.ANY_SOURCE) + " -- " + str(MPI.ANY_TAG))
# First check if a message is present, otherwise we would have to do busy polling
"""
while not comm.Iprobe(source=MPI.ANY_SOURCE, tag=MPI.ANY_TAG, status=status):
# Yield to other threads
# 0 to mean that we should yield elsewhere, UNLESS no other process is waiting
time.sleep(0)
"""
data = comm.recv(source=MPI.ANY_SOURCE, tag=MPI.ANY_TAG, status=status)
tag = status.Get_tag()
assert debug("Got data from " + str(status.Get_source()) + " (" + str(status.Get_tag()) + "): " + str(data))
if tag == 0:
# Flush all waiters, as we will never receive an answer when we close the receiver...
for i in MPIRedirect.waiting:
try:
i.set()
except AttributeError:
# It was not a lock...
pass
except KeyError:
# It was deleted in the meantime
pass
break
elif tag == 1:
# NOTE Go back to listening ASAP, so do the processing on another thread
if data[1] == "getTargets":
# This should not be queued as it might be called recursively and we want to prevent this
threading.Thread(target=Server.processMPI, args=[self, list(data), comm, status.Get_source()]).start()
else:
self.threadpool.add_task(Server.processMPI, self, list(data), comm, status.Get_source())
else:
# Receiving an answer to a previous request
try:
event = MPIRedirect.waiting[tag]
MPIRedirect.waiting[tag] = data
event.set()
except KeyError:
# Probably processed elsewhere already, just skip
pass
except AttributeError:
# Key was already set elsewhere
pass
def bootMPI(self):
if COMM_WORLD.Get_size() > 1:
listener = threading.Thread(target=Server.listenMPI, args=[self])
listener.start()
def setCoupledModel(self, modelCode, parent_location, parent_id, imports = None):
if isinstance(modelCode, str):
# Put this here for Python3
model = None
if imports is not None:
exec(imports)
model = eval(modelCode)
else:
model = modelCode
if not isinstance(model, CoupledDEVS):
raise DEVSException("Only coupled models are allowed to be distributed")
model.internal = False
model.location = self.name
model.setRootSim(self, self.name)
示例13: Mailer
# 需要导入模块: from threadpool import ThreadPool [as 别名]
# 或者: from threadpool.ThreadPool import add_task [as 别名]
class Mailer (object):
MAX_TRIES = 3
DELAY_AFTER_FAILURES = 2
def __init__(self, smtp_server, smtp_port, num_threads, queue_timeout, default_sender, ioloop=tornado.ioloop.IOLoop.current()):
self.threadpool = ThreadPool(
poolname='Mailer',
thread_global_data=self,
thread_quit_hook=self._quit_smtp,
num_threads=num_threads,
queue_timeout=queue_timeout)
self.smtp_server = smtp_server
self.smtp_port = smtp_port
self.default_sender = default_sender
self.ioloop = ioloop
def _create_smtp(self):
"""This method is executed in a worker thread.
Initializes the per-thread state. In this case we create one
smtp per-thread.
"""
smtp = smtplib.SMTP(self.smtp_server, self.smtp_port)
return smtp
@staticmethod
def _quit_smtp(global_data, local_data):
smtp = local_data.smtp
if smtp is not None:
smtp.quit()
def send(self, receivers, subject, body, sender=None, reply_to=None, callback=None):
self.threadpool.add_task(
partial(self._send, sender or self.default_sender, receivers, subject, body, reply_to),
callback
)
def _send(self, sender, receivers, subject, body, reply_to=None, global_data=None, local_data=None):
try:
for i in range(self.MAX_TRIES, 0, -1):
log_email.debug('sending: try=%d, to=%s, subj=%s', self.MAX_TRIES - i + 1, receivers, subject)
try:
smtp = local_data.smtp if hasattr(local_data, 'smtp') else None
if smtp is None:
smtp = global_data._create_smtp()
local_data.smtp = smtp
msg = MIMEMultipart("alternative")
msg["Subject"] = subject
if reply_to is not None:
msg['reply-to'] = reply_to
part1 = MIMEText(body, "plain", "utf-8")
msg.attach(part1)
smtp.sendmail(sender,
receivers,
msg.as_string().encode('ascii')
)
log_email.debug('mail sent succesfully')
return True
except smtplib.SMTPException as e:
if i == 1:
raise e
# global_data.quit_smtp()
local_data.smtp = None
time.sleep(self.DELAY_AFTER_FAILURES)
except:
etype, evalue, tb = sys.exc_info()
log_email.error('can\'t send mail: subject=%s, cause=%s/%s', subject, etype, evalue)
log_email.debug('email body: %s', body)
log_email.debug('full stacktrace:\n%s', loglib.TracebackFormatter(tb))
return False
示例14: Tasks
# 需要导入模块: from threadpool import ThreadPool [as 别名]
# 或者: from threadpool.ThreadPool import add_task [as 别名]
class Tasks(Codes):
def __init__(self):
self.operate = Operate()
self._api = OpenApi()
self._http = HttpClient.getInstance()
self._pool = ThreadPool(5) # 初始化5个线程
print("Task Class 初始化完毕")
def getAllAdmin(self):
print("所有管理员: %s", variable.Admins)
return variable.Admins
def getAllGroup(self):
print("所有关注群: %s", variable.Groups)
return variable.Groups
def addAdmin(self, qq):
return self.operate.addAdmin(qq)
def delAdmin(self, qq):
return self.operate.delAdmin(qq)
def isAdmin(self, qq):
return self.operate.isAdmin(qq)
def addGroup(self, qq):
return self.operate.addGroup(qq)
def delGroup(self, qq):
return self.operate.delGroup(qq)
def inGroup(self, qq):
# print("inGroup: %s", qq)
return self.operate.inGroup(qq)
def addAsk(self, question, answer):
return self.operate.addAsk(question, answer)
def delAsk(self, Id):
return self.operate.delAsk(Id)
def getAsk(self, content):
return self.operate.getAsk(content)
def end(self):
self._pool.destroy()
def uin_to_qq(self, uin):
if uin in variable.UsersQQ:
return variable.UsersQQ.get(uin)
print("获取qq %s %s %s", uin, variable.Vfwebqq, variable.Referer)
html = self._http.get(variable.Get_friend_uin2.format(uin, self.bytesToStr(variable.Vfwebqq)), referer = variable.Referer)
print("uin_to_qq: %s", html)
try:
result = json.loads(self.bytesToStr(html))
if result.get("retcode") != 0:
return ""
qq = result.get("result").get("account")
if qq:
variable.UsersQQ[uin] = str(qq)
return str(qq)
except Exception as e:
print(e)
return ""
def sendMsg(self, *args, **kwargs):
print("回复消息")
url = kwargs.get("url")
data = kwargs.get("data")
# print(data)
referer = kwargs.get("referer")
result = self._http.post(url = url, data = data, referer = referer)
print("回复结果: %s", result)
def otherMsg(self, content, to, url, uin):
if content:
html = self._http.get(url = variable.RobotUrl.format(quote(content), uin))
html = html.replace("\\n", "").replace("\n", "")
html = self._api.parse(html)
html = self._api.getResult()
if html:
print("智能回复: ", html)
data = {'r' : variable.Msg_Data.format(to, uin, html, variable.Clientid, variable.Msgid, variable.Psessionid)}
print(data)
self._pool.add_task(callback = self.sendMsg, url = url, data = data, referer = variable.Referer)
def analyze(self, qq, uin, content, iseq = None):
print("开始解析消息")
if iseq:
print("消息来自群")
to = "group_uin"
url = variable.Send_qun_msg2
else:
print("消息来自好友")
to = "to"
url = variable.Send_buddy_msg2
# 是管理员
if self.isAdmin(qq) and content in ("开启机器人", "关闭机器人", "退出"):
# 解析管理员命令
#.........这里部分代码省略.........
示例15: Server
# 需要导入模块: from threadpool import ThreadPool [as 别名]
# 或者: from threadpool.ThreadPool import add_task [as 别名]
#.........这里部分代码省略.........
:param comm: the MPI COMM object
:param remote: the location from where the message was received
"""
# Receiving a new request
resendTag = data[0]
function = data[1]
args = data[2]
kwargs = data[3]
result = getattr(self, function)(*args, **kwargs)
if resendTag is not None:
if result is None:
result = 0
comm.send(result, dest=remote, tag=resendTag)
def listenMPI(self):
"""
Listen for incomming MPI messages and process them as soon as they are received
"""
comm = middleware.COMM_WORLD
status = middleware.MPI.Status()
while 1:
#assert debug("[" + str(comm.Get_rank()) + "]Listening to remote " + str(middleware.MPI.ANY_SOURCE) + " -- " + str(middleware.MPI.ANY_TAG))
# First check if a message is present, otherwise we would have to do busy polling
data = comm.recv(source=middleware.MPI.ANY_SOURCE, tag=middleware.MPI.ANY_TAG, status=status)
tag = status.Get_tag()
#assert debug("Got data from " + str(status.Get_source()) + " (" + str(status.Get_tag()) + "): " + str(data))
if tag == 0:
# Flush all waiters, as we will never receive an answer when we close the receiver...
self.finishWaitingPool()
break
elif tag == 1:
# NOTE Go back to listening ASAP, so do the processing on another thread
if data[1] == "receive" or data[1] == "receiveAntiMessages":
self.threadpool.add_task(Server.processMPI, self, list(data), comm, status.Get_source())
else:
# Normal 'control' commands are immediately executed, as they would otherwise have the potential to deadlock the node
threading.Thread(target=Server.processMPI, args=[self, list(data), comm, status.Get_source()]).start()
else:
# Receiving an answer to a previous request
try:
event = MPIRedirect.waiting[tag]
MPIRedirect.waiting[tag] = data
event.set()
except KeyError:
# Probably processed elsewhere already, just skip
pass
except AttributeError:
# Key was already set elsewhere
pass
def finishWaitingPool(self):
"""
Stop the complete MPI request queue from blocking, used when stopping simulation is necessary while requests are still outstanding.
"""
for i in MPIRedirect.waiting:
try:
i.set()
except AttributeError:
# It was not a lock...
pass
except KeyError:
# It was deleted in the meantime
pass
def bootMPI(self):
"""