Äú¿ÉÒÔ¾èÖú£¬Ö§³ÖÎÒÃǵĹ«ÒæÊÂÒµ¡£

1Ôª 10Ôª 50Ôª





ÈÏÖ¤Â룺  ÑéÖ¤Âë,¿´²»Çå³þ?Çëµã»÷Ë¢ÐÂÑéÖ¤Âë ±ØÌî



  ÇóÖª ÎÄÕ ÎÄ¿â Lib ÊÓÆµ iPerson ¿Î³Ì ÈÏÖ¤ ×Éѯ ¹¤¾ß ½²×ù Modeler   Code  
»áÔ±   
 
   
 
 
     
   
 ¶©ÔÄ
  ¾èÖú
Python²¢·¢±à³ÌÖ®Ï̳߳Ø/½ø³Ì³Ø
 
À´Ô´£ºZiWenXie ·¢²¼ÓÚ£º 2017-2-8
  1996  次浏览      27
 

ÒýÑÔ

Python±ê×¼¿âΪÎÒÃÇÌṩÁËthreadingºÍmultiprocessingÄ£¿é±àдÏàÓ¦µÄ¶àÏß³Ì/¶à½ø³Ì´úÂ룬µ«Êǵ±ÏîÄ¿´ïµ½Ò»¶¨µÄ¹æÄ££¬Æµ·±´´½¨/Ïú»Ù½ø³Ì»òÕßÏß³ÌÊǷdz£ÏûºÄ×ÊÔ´µÄ£¬Õâ¸öʱºòÎÒÃǾÍÒª±àд×Ô¼ºµÄÏ̳߳Ø/½ø³Ì³Ø£¬ÒԿռ任ʱ¼ä¡£µ«´ÓPython3.2¿ªÊ¼£¬±ê×¼¿âΪÎÒÃÇÌṩÁËconcurrent.futuresÄ£¿é£¬ËüÌṩÁËThreadPoolExecutorºÍProcessPoolExecutorÁ½¸öÀ࣬ʵÏÖÁ˶ÔthreadingºÍmultiprocessingµÄ½øÒ»²½³éÏ󣬶ԱàдÏ̳߳Ø/½ø³Ì³ØÌṩÁËÖ±½ÓµÄÖ§³Ö¡£

ExecutorºÍFuture

concurrent.futuresÄ£¿éµÄ»ù´¡ÊÇExectuor£¬ExecutorÊÇÒ»¸ö³éÏóÀ࣬Ëü²»Äܱ»Ö±½ÓʹÓᣵ«ÊÇËüÌṩµÄÁ½¸ö×ÓÀàThreadPoolExecutorºÍProcessPoolExecutorÈ´ÊǷdz£ÓÐÓ㬹ËÃû˼ÒåÁ½Õß·Ö±ð±»ÓÃÀ´´´½¨Ï̳߳غͽø³Ì³ØµÄ´úÂë¡£ÎÒÃÇ¿ÉÒÔ½«ÏàÓ¦µÄtasksÖ±½Ó·ÅÈëÏ̳߳Ø/½ø³Ì³Ø£¬²»ÐèҪά»¤QueueÀ´²ÙÐÄËÀËøµÄÎÊÌ⣬Ï̳߳Ø/½ø³Ì³Ø»á×Ô¶¯°ïÎÒÃǵ÷¶È¡£

FutureÕâ¸ö¸ÅÄîÏàÐÅÓÐjavaºÍnodejsϱà³Ì¾­ÑéµÄÅóÓѿ϶¨²»Ä°ÉúÁË£¬Äã¿ÉÒÔ°ÑËüÀí½âΪһ¸öÔÚδÀ´Íê³ÉµÄ²Ù×÷£¬ÕâÊÇÒì²½±à³ÌµÄ»ù´¡£¬´«Í³±à³ÌģʽϱÈÈçÎÒÃDzÙ×÷queue.getµÄʱºò£¬Ôڵȴý·µ»Ø½á¹û֮ǰ»á²úÉú×èÈû£¬cpu²»ÄÜÈóöÀ´×öÆäËûÊÂÇ飬¶øFutureµÄÒýÈë°ïÖúÎÒÃÇÔڵȴýµÄÕâ¶Îʱ¼ä¿ÉÒÔÍê³ÉÆäËûµÄ²Ù×÷¡£¹ØÓÚÔÚPythonÖнøÐÐÒì²½IO¿ÉÒÔÔĶÁÍê±¾ÎÄÖ®ºó²Î¿¼ÎÒµÄPython²¢·¢±à³Ì֮Э³Ì/Òì²½IO¡£

p.s: Èç¹ûÄãÒÀÈ»ÔÚ¼áÊØPython2.x£¬ÇëÏȰ²×°futuresÄ£¿é¡£

pip install futures 

ʹÓÃsubmitÀ´²Ù×÷Ï̳߳Ø/½ø³Ì³Ø

ÎÒÃÇÏÈͨ¹ýÏÂÃæÕâ¶Î´úÂëÀ´Á˽âÒ»ÏÂÏ̳߳صĸÅÄî

# example1.py 
from concurrent.futures import ThreadPoolExecutor
import time
def return_future_result(message):
time.sleep(2)
return message
pool = ThreadPoolExecutor(max_workers=2)
# ´´½¨Ò»¸ö×î´ó¿ÉÈÝÄÉ2¸ötaskµÄÏ̳߳Ø
future1 = pool.submit(return_future_result, ("hello"))
# ÍùÏ̳߳ØÀïÃæ¼ÓÈëÒ»¸ötask
future2 = pool.submit(return_future_result, ("world"))
# ÍùÏ̳߳ØÀïÃæ¼ÓÈëÒ»¸ötask
print(future1.done()) # ÅжÏtask1ÊÇ·ñ½áÊø
time.sleep(3)
print(future2.done()) # ÅжÏtask2ÊÇ·ñ½áÊø
print(future1.result()) # ²é¿´task1·µ»ØµÄ½á¹û
print(future2.result()) # ²é¿´task2·µ»ØµÄ½á¹û

ÎÒÃǸù¾ÝÔËÐнá¹ûÀ´·ÖÎöһϡ£ÎÒÃÇʹÓÃsubmit·½·¨À´ÍùÏ̳߳ØÖмÓÈëÒ»¸ötask£¬submit·µ»ØÒ»¸öFuture¶ÔÏ󣬶ÔÓÚFuture¶ÔÏó¿ÉÒÔ¼òµ¥µØÀí½âΪһ¸öÔÚδÀ´Íê³ÉµÄ²Ù×÷¡£ÔÚµÚÒ»¸öprintÓï¾äÖкÜÃ÷ÏÔÒòΪtime.sleep(2)µÄÔ­ÒòÎÒÃǵÄfuture1ûÓÐÍê³É£¬ÒòΪÎÒÃÇʹÓÃtime.sleep(3)ÔÝÍ£ÁËÖ÷Ị̈߳¬ËùÒÔµ½µÚ¶þ¸öprintÓï¾äµÄʱºòÎÒÃÇÏ̳߳ØÀïµÄÈÎÎñ¶¼ÒѾ­È«²¿½áÊø¡£

ziwenxie :: ~ ? python example1.py 
False
True
hello
world
# ÔÚÉÏÊö³ÌÐòÖ´ÐеĹý³ÌÖУ¬
ͨ¹ýpsÃüÁîÎÒÃÇ¿ÉÒÔ¿´µ½Èý¸öÏß³ÌͬʱÔÚºǫ́ÔËÐÐ
ziwenxie :: ~ ? ps -eLf | grep python
ziwenxie 8361 7557 8361 3 3 19:45 pts/0 00:00:00 python example1.py
ziwenxie 8361 7557 8362 0 3 19:45 pts/0 00:00:00 python example1.py
ziwenxie 8361 7557 8363 0 3 19:45 pts/0 00:00:00 python example1.py

ÉÏÃæµÄ´úÂëÎÒÃÇÒ²¿ÉÒÔ¸ÄдΪ½ø³Ì³ØÐÎʽ£¬apiºÍÏ̳߳ØÈç³öÒ»ÕÞ£¬ÎҾͲ»ÂÞàÂÁË¡£

# example2.py 
from concurrent.futures import ProcessPoolExecutor
import time
def return_future_result(message):
time.sleep(2)
return message
pool = ProcessPoolExecutor(max_workers=2)
future1 = pool.submit(return_future_result, ("hello"))
future2 = pool.submit(return_future_result, ("world"))
print(future1.done())
time.sleep(3)
print(future2.done())
print(future1.result())
print(future2.result())

ÏÂÃæÊÇÔËÐнá¹û

ziwenxie :: ~ ? python example2.py 
False
True
hello
world
ziwenxie :: ~ ? ps -eLf | grep python
ziwenxie 8560 7557 8560 3 3 19:53 pts/0 00:00:00 python example2.py
ziwenxie 8560 7557 8563 0 3 19:53 pts/0 00:00:00 python example2.py
ziwenxie 8560 7557 8564 0 3 19:53 pts/0 00:00:00 python example2.py
ziwenxie 8561 8560 8561 0 1 19:53 pts/0 00:00:00 python example2.py
ziwenxie 8562 8560 8562 0 1 19:53 pts/0 00:00:00 python example2.py

ʹÓÃmap/waitÀ´²Ù×÷Ï̳߳Ø/½ø³Ì³Ø

³ýÁËsubmit£¬Exectuor»¹ÎªÎÒÃÇÌṩÁËmap·½·¨£¬ºÍÄÚ½¨µÄmapÓ÷¨ÀàËÆ£¬ÏÂÃæÎÒÃÇͨ¹ýÁ½¸öÀý×ÓÀ´±È½ÏÒ»ÏÂÁ½ÕßµÄÇø±ð¡£

ʹÓÃsubmit²Ù×÷»Ø¹Ë

# example3.py 
import concurrent.futures
import urllib.request
URLS = ['http://httpbin.org', 'http://example.com/', 'https://api.github.com/']
def load_url(url, timeout):
with urllib.request.urlopen(url, timeouttimeout=timeout) as conn:
return conn.read()
# We can use a with statement to ensure threads are cleaned up promptly
with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:
# Start the load operations and mark each future with its URL
future_to_url = {executor.submit(load_url, url, 60): url for url in URLS}
for future in concurrent.futures.as_completed(future_to_url):
url = future_to_url[future]
try:
data = future.result()
except Exception as exc:
print('%r generated an exception: %s' % (url, exc))
else:
print('%r page is %d bytes' % (url, len(data)))

´ÓÔËÐнá¹û¿ÉÒÔ¿´³ö£¬as_completed²»Êǰ´ÕÕURLSÁбíÔªËØµÄ˳Ðò·µ»ØµÄ¡£

ziwenxie :: ~ ? python example3.py 
'http://example.com/' page is 1270 byte
'https://api.github.com/' page is 2039 bytes
'http://httpbin.org' page is 12150 bytes

ʹÓÃmap

# example4.py 
import concurrent.futures
import urllib.request
URLS = ['http://httpbin.org', 'http://example.com/', 'https://api.github.com/']
def load_url(url):
with urllib.request.urlopen(url, timeout=60) as conn:
return conn.read()
# We can use a with statement to ensure threads are cleaned up promptly
with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:
for url, data in zip(URLS, executor.map(load_url, URLS)):
print('%r page is %d bytes' % (url, len(data)))

´ÓÔËÐнá¹û¿ÉÒÔ¿´³ö£¬mapÊǰ´ÕÕURLSÁбíÔªËØµÄ˳Ðò·µ»ØµÄ£¬²¢ÇÒд³öµÄ´úÂë¸ü¼Ó¼ò½àÖ±¹Û£¬ÎÒÃÇ¿ÉÒÔ¸ù¾Ý¾ßÌåµÄÐèÇóÈÎѡһÖÖ¡£

ziwenxie :: ~ ? python example4.py 
'http://httpbin.org' page is 12150 bytes
'http://example.com/' page is 1270 bytes
'https://api.github.com/' page is 2039 bytes

µÚÈýÖÖÑ¡Ôñwait

wait·½·¨½Ó»á·µ»ØÒ»¸ötuple(Ôª×é)£¬tupleÖаüº¬Á½¸öset(¼¯ºÏ)£¬Ò»¸öÊÇcompleted(ÒÑÍê³ÉµÄ)ÁíÍâÒ»¸öÊÇuncompleted(δÍê³ÉµÄ)¡£Ê¹ÓÃwait·½·¨µÄÒ»¸öÓÅÊÆ¾ÍÊÇ»ñµÃ¸ü´óµÄ×ÔÓɶȣ¬Ëü½ÓÊÕÈý¸ö²ÎÊýFIRST_COMPLETED, FIRST_EXCEPTION ºÍALL_COMPLETE£¬Ä¬ÈÏÉèÖÃΪALL_COMPLETED¡£

ÎÒÃÇͨ¹ýÏÂÃæÕâ¸öÀý×ÓÀ´¿´Ò»ÏÂÈý¸ö²ÎÊýµÄÇø±ð

from concurrent.futures import ThreadPoolExecutor, wait, as_completed 
from time import sleep
from random import randint
def return_after_random_secs(num):
sleep(randint(1, 5))
return "Return of {}".format(num)
pool = ThreadPoolExecutor(5)
futures = []
for x in range(5):
futures.append(pool.submit(return_after_random_secs, x))
print(wait(futures))
# print(wait(futures, timeout=None, return_when='FIRST_COMPLETED'))!

Èç¹û²ÉÓÃĬÈϵÄALL_COMPLETED£¬³ÌÐò»á×èÈûÖ±µ½Ï̳߳ØÀïÃæµÄËùÓÐÈÎÎñ¶¼Íê³É¡£

ziwenxie :: ~ ? python example5.py
DoneAndNotDoneFutures(done={
<Future at 0x7f0b06c9bc88 state=finished returned str>,
<Future at 0x7f0b06cbaa90 state=finished returned str>,
<Future at 0x7f0b06373898 state=finished returned str>,
<Future at 0x7f0b06352ba8 state=finished returned str>,
<Future at 0x7f0b06373b00 state=finished returned str>}, not_done=set())

Èç¹û²ÉÓÃFIRST_COMPLETED²ÎÊý£¬³ÌÐò²¢²»»áµÈµ½Ï̳߳ØÀïÃæËùÓеÄÈÎÎñ¶¼Íê³É¡£

ziwenxie :: ~ ? python example5.py 
DoneAndNotDoneFutures(done={
<Future at 0x7f84109edb00 state=finished returned str>,
<Future at 0x7f840e2e9320 state=finished returned str>,
<Future at 0x7f840f25ccc0 state=finished returned str>},
not_done={<Future at 0x7f840e2e9ba8 state=running>,
<Future at 0x7f840e2e9940 state=running>})

˼¿¼Ìâ

дһ¸öС³ÌÐò¶Ô±Èmultiprocessing.pool(ThreadPool)ºÍProcessPollExecutor(ThreadPoolExecutor)ÔÚÖ´ÐÐЧÂÊÉϵIJî¾à£¬½áºÏÉÏÃæÌáµ½µÄFuture˼¿¼ÎªÊ²Ã´»áÔì³ÉÕâÑùµÄ½á¹û¡£

   
1996 ´Îä¯ÀÀ       27
 
Ïà¹ØÎÄÕÂ

ÊÖ»úÈí¼þ²âÊÔÓÃÀýÉè¼ÆÊµ¼ù
ÊÖ»ú¿Í»§¶ËUI²âÊÔ·ÖÎö
iPhoneÏûÏ¢ÍÆËÍ»úÖÆÊµÏÖÓë̽ÌÖ
AndroidÊÖ»ú¿ª·¢£¨Ò»£©
 
Ïà¹ØÎĵµ

Android_UI¹Ù·½Éè¼Æ½Ì³Ì
ÊÖ»ú¿ª·¢Æ½Ì¨½éÉÜ
androidÅÄÕÕ¼°ÉÏ´«¹¦ÄÜ
Android½²ÒåÖÇÄÜÊÖ»ú¿ª·¢
Ïà¹Ø¿Î³Ì

Android¸ß¼¶Òƶ¯Ó¦ÓóÌÐò
Androidϵͳ¿ª·¢
AndroidÓ¦Óÿª·¢
ÊÖ»úÈí¼þ²âÊÔ
×îл¼Æ»®
DeepSeekÔÚÈí¼þ²âÊÔÓ¦ÓÃʵ¼ù 4-12[ÔÚÏß]
DeepSeek´óÄ£ÐÍÓ¦Óÿª·¢Êµ¼ù 4-19[ÔÚÏß]
UAF¼Ü¹¹ÌåϵÓëʵ¼ù 4-11[±±¾©]
AIÖÇÄÜ»¯Èí¼þ²âÊÔ·½·¨Óëʵ¼ù 5-23[ÉϺ£]
»ùÓÚ UML ºÍEA½øÐзÖÎöÉè¼Æ 4-26[±±¾©]
ÒµÎñ¼Ü¹¹Éè¼ÆÓ뽨ģ 4-18[±±¾©]

androidÈË»ú½çÃæÖ¸ÄÏ
AndroidÊÖ»ú¿ª·¢£¨Ò»£©
AndroidÊÖ»ú¿ª·¢£¨¶þ£©
AndroidÊÖ»ú¿ª·¢£¨Èý£©
AndroidÊÖ»ú¿ª·¢£¨ËÄ£©
iPhoneÏûÏ¢ÍÆËÍ»úÖÆÊµÏÖ̽ÌÖ
ÊÖ»úÈí¼þ²âÊÔÓÃÀýÉè¼ÆÊµ¼ù
ÊÖ»ú¿Í»§¶ËUI²âÊÔ·ÖÎö
ÊÖ»úÈí¼þ×Ô¶¯»¯²âÊÔÑо¿±¨¸æ


Android¸ß¼¶Òƶ¯Ó¦ÓóÌÐò
AndroidÓ¦Óÿª·¢
Androidϵͳ¿ª·¢
ÊÖ»úÈí¼þ²âÊÔ
ǶÈëʽÈí¼þ²âÊÔ
AndroidÈí¡¢Ó²¡¢ÔÆÕûºÏ


ÁìÏÈIT¹«Ë¾ android¿ª·¢Æ½Ì¨×î¼Ñʵ¼ù
±±¾© Android¿ª·¢¼¼Êõ½ø½×
ijÐÂÄÜÔ´ÁìÓòÆóÒµ Android¿ª·¢¼¼Êõ
ijº½Ì칫˾ Android¡¢IOSÓ¦ÓÃÈí¼þ¿ª·¢
°¢¶û¿¨ÌØ LinuxÄÚºËÇý¶¯
°¬Ä¬Éú ǶÈëʽÈí¼þ¼Ü¹¹Éè¼Æ
Î÷ÃÅ×Ó Ç¶Èëʽ¼Ü¹¹Éè¼Æ