Upgrade to Pro — share decks privately, control downloads, hide ads and more …

asyncioを軽くさわってみた

Satoshi Miura
September 15, 2015

 asyncioを軽くさわってみた

第28回Python東海の発表資料

Satoshi Miura

September 15, 2015
Tweet

More Decks by Satoshi Miura

Other Decks in Technology

Transcript

  1. 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.
  2. 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") }
  3. asyncio asyncio - ඇಉظ I/OɺΠϕϯτϧʔϓɺίϧʔνϯ͓ΑͼλεΫ • Python3.4Ͱ௥Ճ͞ΕͨඇಉظI/Oͳඪ४ϥΠϒϥϦ • Python3.3Ͱ΋࢖༻Մೳ #

    pip install asyncio • PyConJP 2014Ͱ΋ηογϣϯ͕͋Γ·ͨ͠ɻ • PythonʹΑΔඇಉظϓϩάϥϛϯάೖ໳ (ja)
  4. 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()
  5. 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