LoginSignup
1
1

More than 5 years have passed since last update.

python での スレッドの使用例

Last updated at Posted at 2017-05-02
thread01.py
#! /usr/bin/python3
# -*- coding: utf-8 -*-
#
# --------------------------------------------------------------------
import threading
import time
import sys
# --------------------------------------------------------------------
def ff01 (aa,bb,cc):
    sys.stderr.write("*** ff01 *** start ***\n")
    for it in range (3):
        print (str (it) + "\t" + aa + "\t" + bb + "\t" +cc)
        it += 1
        time.sleep(1)
    sys.stderr.write("*** ff01 *** end ***\n")
#
# --------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
th1 = threading.Thread(target=ff01,args=("hello","Good","Morning"))
th1.setDaemon(True)
th1.start()
#
try:
    icount = 0
    while True:
        sys.stderr.write("icount = %d\n" % icount)
        time.sleep(3)
        icount += 1
except KeyboardInterrupt:
    print ("STOP")
#
sys.stderr.write("*** 終了 ***\n")
# --------------------------------------------------------------------

実行結果

$ ./thread01.py 
*** 開始 ***
*** ff01 *** start ***
0   hello   Good    Morning
icount = 0
1   hello   Good    Morning
2   hello   Good    Morning
icount = 1
*** ff01 *** end ***
icount = 2
icount = 3
icount = 4
icount = 5
^CSTOP
*** 終了 ***

次の環境で動作を確認

Arch Linux (4.15.15-1-ARCH)

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1