内容へ移動
fl8 Wiki
ユーザ用ツール
ログイン
サイト用ツール
検索
ツール
文書の表示
以前のリビジョン
バックリンク
最近の変更
メディアマネージャー
サイトマップ
ログイン
>
最近の変更
メディアマネージャー
サイトマップ
現在位置:
Dokuwiki.fl8.jp
»
15_python
»
04 Python 並列処理
トレース:
15_python:04_parallel
この文書は読取専用です。文書のソースを閲覧することは可能ですが、変更はできません。もし変更したい場合は管理者に連絡してください。
====== 04 Python 並列処理 ====== <code|parallel.py> import time from concurrent.futures import ThreadPoolExecutor from logging import StreamHandler, Formatter, INFO, getLogger def init_logger(): handler = StreamHandler() handler.setLevel(INFO) handler.setFormatter(Formatter("[%(asctime)s] [%(threadName)s] %(message)s")) logger = getLogger() logger.addHandler(handler) logger.setLevel(INFO) def task(v): getLogger().info("%s start", v) time.sleep(1.0) getLogger().info("%s end", v) def main(): init_logger() getLogger().info("main start") with ThreadPoolExecutor(max_workers=2, thread_name_prefix="thread") as executor: for i in range(5): executor.submit(task, i) getLogger().info("submit end") getLogger().info("main end") if __name__ == "__main__": main() </code> ===== 実行結果 ===== <code> # python3.6 parallel.py [2019-08-09 11:23:41,298] [MainThread] main start [2019-08-09 11:23:41,300] [thread_0] 0 start [2019-08-09 11:23:41,301] [thread_1] 1 start [2019-08-09 11:23:41,301] [MainThread] submit end [2019-08-09 11:23:42,302] [thread_0] 0 end [2019-08-09 11:23:42,302] [thread_0] 2 start [2019-08-09 11:23:42,302] [thread_1] 1 end [2019-08-09 11:23:42,302] [thread_1] 3 start [2019-08-09 11:23:43,303] [thread_0] 2 end [2019-08-09 11:23:43,304] [thread_0] 4 start [2019-08-09 11:23:43,304] [thread_1] 3 end [2019-08-09 11:23:44,305] [thread_0] 4 end [2019-08-09 11:23:44,305] [MainThread] main end </code> {{tag>python}}
15_python/04_parallel.txt
· 最終更新: 2019/08/09 02:27 by
matsui
ページ用ツール
文書の表示
以前のリビジョン
バックリンク
文書の先頭へ