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
350
FlaskとYomanでHello World
mursts
0
400
Google App Engine入門
mursts
0
660
Other Decks in Technology
See All in Technology
はじめてのWDM
miyukichi_ospf
1
150
OPENLOGI Company Profile for engineer
hr01
1
74k
Amazon EVS で VCF 9.0 / 9.1 のサポート開始まとめ
mtoyoda
0
310
ruby.wasmとPicoRuby.wasmに対応した仮想DOMライブラリを作ってる話 #kaigieffect_kaigi
sue445
PRO
0
150
SoccerMaster: A Vision Foundation Model for Soccer Understanding
kzykmyzw
0
140
AIコード生成×サプライチェーン攻撃 — PHPが直面する“二重の信頼問題
shinyasaita
0
190
Making sense of Google’s agentic dev tools
glaforge
1
280
マルチアカウント環境でSecurity Hubの運用、その後どうなった? / SRE NEXT 2026 miniLT会
genda
0
100
オブザーバビリティ、本当に活用できてる? 〜API連携×生成AIで成熟度を自動評価〜
dmmsre
1
3.5k
“それは自分の仕事じゃない"を越えて行け
yuukiyo
1
470
LLMやAIエージェントをソフトウェアに組み込むプラクティス
shibuiwilliam
2
420
AIが実装を自走する時代の認知負債との戦い
lycorptech_jp
PRO
0
140
Featured
See All Featured
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
1
420
What's in a price? How to price your products and services
michaelherold
247
13k
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
220
Mind Mapping
helmedeiros
PRO
1
280
Discover your Explorer Soul
emna__ayadi
2
1.2k
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
260
Rails Girls Zürich Keynote
gr2m
96
14k
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
420
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
3k
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.5k
The Invisible Side of Design
smashingmag
301
52k
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Ͱಋೖ͞ΕΔܕώϯτָ͠ΈͰ͢
͝੩ௌ͋Γ͕ͱ͏͍͟͝ ·ͨ͠