"utf file header" : {
"prefix" : " utf" ,
"body" : [
" # -*- coding: utf-8 -*-" ,
" " ,
" $0"
],
"description" : " utf file header"
},
"get soup" : {
"prefix" : " gsoup" ,
"body" : [
" res = requests.get(url)" ,
" res.encoding = 'utf-8'" ,
" html = res.text" ,
" page = BeautifulSoup(html, 'lxml')" ,
" $1"
],
"description" : " get BeautifulSoup"
}
多进程+多线程写法 返回值为True则结束所有子进程
p = Pool (processes = 5 )
# 多进程写法1 返回值为True则结束所有子进程,117s
result = queue .Queue ()
def pool_th ():
for pwd in pwds :
try :
result .put (p .apply_async (un_encrypted_rar , args = (rar , pwd )))
except :
break
def result_th ():
while 1 :
a = result .get ().get ()
if a :
p .terminate ()
break
'''
利用多线程,同事运行P函数创建执行子进程,以及运行获取子进程返回值函数
'''
t1 = threading .Thread (target = pool_th )
t2 = threading .Thread (target = result_th )
t1 .start ()
t2 .start ()
t1 .join ()
t2 .join ()
p .join ()
import time
def clock (func ):
def clocked (* args ):
t0 = time .time ()
result = func (* args )
elapsed = time .time () - t0
name = func .__name__
arg_str = ', ' .join (repr (arg ) for arg in args )
print (f'[{ elapsed :.8f} ] { name } ({ arg_str } ) -> { result } ' )
return result
return clocked