Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
asyncioを軽くさわってみた
Search
Satoshi Miura
September 15, 2015
Technology
180
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
asyncioを軽くさわってみた
第28回Python東海の発表資料
Satoshi Miura
September 15, 2015
More Decks by Satoshi Miura
See All by Satoshi Miura
MarkdownでもSphinx
mursts
1
470
お金ダッシュボードを作ってみた
mursts
0
350
FlaskとYomanでHello World
mursts
0
400
Google App Engine入門
mursts
0
660
Other Decks in Technology
See All in Technology
Bits AI を制するものは Datadog を制す / The player that controls Bits AI, controls Datadog
kaminashi
0
130
QAエンジニア起点で進める、SmartHRにおける信頼性向上について
kaomi_wombat
1
150
LLM Internals: 언어 모델의 계보와 알고리즘 진화 (2023~2026)
inureyes
PRO
0
700
My broken English still works: speaking at global OSS events
naruoga
0
110
ボトムアップ文化が強い組織で セキュリティをどう根付かせていくかの現在進行形の話 / Making Security Stick in a Bottom-Up Organization
yamaguchitk333
0
250
LLM・AIエージェントシステムベストプラクティス
shibuiwilliam
6
1.5k
AWS ネットワーク構築でハマった(ハマりかけた) 5選とそこから得た教訓
nagisa53
4
280
What's new in Go 1.27?
ciarana
0
260
モバイルアプリ開発概論2026
recruitengineers
PRO
6
670
Digitization部 紹介資料
sansan33
PRO
2
7.7k
JavaScript 研修 (2026)
recruitengineers
PRO
2
630
エンドユーザー視点で見る SansanのMeraki活用と内製自動化
sansantech
PRO
0
120
Featured
See All Featured
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
10k
Exploring anti-patterns in Rails
aemeredith
3
470
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
2
640
How to build a perfect <img>
jonoalderson
1
5.9k
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
420
Darren the Foodie - Storyboard
khoart
PRO
3
3.7k
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.8k
Mind Mapping
helmedeiros
PRO
1
320
Scaling GitHub
holman
464
140k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
34
2.8k
So, you think you're a good person
axbom
PRO
2
2.1k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
470
Transcript
asyncioΛઙ͘͞Θͬͯ Έͨ 2015.09.12 Python౦ւ 28th
͓લͩΕΑ • Έ͏Β͞ͱ͠(@mursts) • Python౦ւͷཧਓͷҰਓͰ͢ • ࠷ۙGoΛ৮Γ࢝Ί·ͨ͠ • Tour of
Go ͚ͩ
࠷ॳʹͪΐͬͱ͚ͩGoͷ
GoͷΑ͍ͱ͜
Gopher͕͔Θ͍͍ ※ݸਓతͳײͰ͢
The Go gopher was designed by Renee French. The gopher
vector data was made by Takuya Ueda. Licensed under the Creative Commons 3.0 Attributions license.
Goroutine͕͋Δ ※ݸਓతͳײͰ͢
Goroutineʁ
Goroutine • GoͰฒߦ࣮ߦΛ࣮ݱ͢ΔΈͰɺฏͨ͘ݴ͏ͱεϨουΈ ͍ͨͳײ͡ • Goroutine͕͋Δ͚ͩͰฒྻॲཧ͕ॻ͖͍͢
Tour of Go #63 package main import ( "fmt" "time"
) func say(s string) { for i := 0; i < 5; i++ { time.Sleep(100 * time.Millisecond) fmt.Println(s) } } func main() { go say("world") say("hello") }
಄ʹgoΛ͚ͭͯݺͿ͚ͩ func main() { go say("world") say("hello") } ͜Ε͚ͩͰผεϨουͰ࣮ߦͰ͖·͢
Goͷ͓͠·͍
͡Ό͋Pythonͷฒྻॲ ཧʁ
PythonͰͷฒྻॲཧ • multiprocessing • concurrent.futures • asyncio etc...
PythonͰͷฒྻॲཧ • multiprocessing • concurrent.futures • asyncio etc...
asyncio asyncio - ඇಉظ I/OɺΠϕϯτϧʔϓɺίϧʔνϯ͓ΑͼλεΫ • Python3.4ͰՃ͞ΕͨඇಉظI/Oͳඪ४ϥΠϒϥϦ • Python3.3Ͱ༻Մೳ #
pip install asyncio • PyConJP 2014Ͱηογϣϯ͕͋Γ·ͨ͠ɻ • PythonʹΑΔඇಉظϓϩάϥϛϯάೖ (ja)
αϯϓϧ
import asyncio import random @asyncio.coroutine def wait_for(task_name): wait = random.randint(1,
5) print('taks {} begin. wait {} sec'.format(task_name, wait)) yield from asyncio.sleep(wait) print('taks {} end.'.format(task_name)) def main(): tasks = [wait_for(x) for x in range(3)] loop = asyncio.get_event_loop() loop.run_until_complete(asyncio.wait(tasks)) loop.stop() if __name__ == '__main__': main()
Πϕϯτϧʔϓ࡞ͬͯɺίϧʔνϯ࡞ͬͯݺͼग़ͤฒྻॲཧ͕ Ͱ͖ͦ͏
͜·͚͍͍͐ͨ͊͜ΜͩΑʂʂ (AAུ
ࠓճΓ͔ͨͬͨ͜ͱ
http://d.hatena.ne.jp/heavenshell/20120304
asyncioΛͬͨ2015 ൛Λʂ
ͬͯΈͨ
σϞ https://gist.github.com/mursts/1709f01572365a98d057
໌(20159݄13)ԿͷͰ͠ΐ͏͔ʁ
Python3.5ϦϦʔε(༧ఆ)ʂ
None
None
import asyncio async def co_func1(): return 'co_func1_ok' async def co_func2():
return await co_func1() + ' co_func2_ok' loop = asyncio.get_event_loop() print(loop.run_until_complete(co_func2())) # co_func1_ok co_func2_ok Python3.5͔Βಋೖ͞ΕΔasyncͱawaitͰίϧʔνϯΛѻ͏ - Qiita
3.5Ͱasyncioͷվྑೖ͍ͬͯΔΑ͏ͳͷͰɺͥͻ3.5Ͱͬ ͯΈ·͠ΐ͏ Python3.4 ͷ৽ػೳ asyncio ΛͬͯΈΔ
3.5Ͱಋೖ͞ΕΔܕώϯτָ͠ΈͰ͢
͝੩ௌ͋Γ͕ͱ͏͍͟͝ ·ͨ͠