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
460
お金ダッシュボードを作ってみた
mursts
0
340
FlaskとYomanでHello World
mursts
0
400
Google App Engine入門
mursts
0
660
Other Decks in Technology
See All in Technology
RAG を使わないという選択肢
tatsutaka
1
200
タクシーアプリ『GO』の実践的データ活用
mot_techtalk
3
190
protovalidate-es を導入してみた
bengo4com
0
170
日本 Fintech 未来予測レポート 2027〜2028年(オリジナル版)
8maki
0
2k
ポケモンの型をTypeScriptの型システムで表現してみた
subroh0508
0
370
NAB Show 2026 動画技術関連レポート / NAB Show 2026 Report
cyberagentdevelopers
PRO
0
170
[モダンアプリ勉強会]今更聞けないGit/GitHub入門
tsukuboshi
0
370
AI駆動開発を通して感じた、 AI時代のデザイナーの役割変化
whisaiyo
1
1.2k
攻撃者視点で考えるDetection Engineering
cryptopeg
2
1.3k
2026TECHFRESH畢業分享會 - Lightning Talk - 打造精準高效的 MCP 設計模式與測試實務
line_developers_tw
PRO
0
870
10倍の生産性を実現するAI駆動並列エージェントのすべて
kumaiu
5
1.4k
AIソロプレナー時代に2ヶ月で20人増員した事業創造会社の開発組織の話
miyatakoji
0
610
Featured
See All Featured
A better future with KSS
kneath
240
18k
Google's AI Overviews - The New Search
badams
0
1k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.7k
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.1k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.7k
Build your cross-platform service in a week with App Engine
jlugia
234
18k
VelocityConf: Rendering Performance Case Studies
addyosmani
333
25k
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
1
5.9k
Context Engineering - Making Every Token Count
addyosmani
9
960
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
2
850
Technical Leadership for Architectural Decision Making
baasie
3
400
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
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Ͱಋೖ͞ΕΔܕώϯτָ͠ΈͰ͢
͝੩ௌ͋Γ͕ͱ͏͍͟͝ ·ͨ͠