top of page

Pythonでスレッド

  • hiro2studio
  • 2021年2月15日
  • 読了時間: 1分

とにかく大事なスレッド処理。簡単なものから。


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

'''
スレッド
'''

import time
import threading

def thread001():
    print("001 start")
    for i in range(10):
        time.sleep(1)
        print("001:" + str(i))
    print("001 end")


def thread002():
    print("002 start")
    for i in range(5):
        time.sleep(2)
        print("002:" + str(i))
    print("002 end")

#-------------------------------------------------
## main ###
#-------------------------------------------------
if __name__=='__main__':

    thread1 = threading.Thread(target=thread001)
    thread2 = threading.Thread(target=thread002)
    thread1.start()
    thread2.start()
    thread1.join()
    thread2.join()




結果はこちら

001 start

002 start

001:0

002:0

001:1

001:2

002:1

001:3

001:4

002:2

001:5

001:6

002:3

001:7

001:8

002:4

002 end

001:9

001 end




 
 
 

最新記事

すべて表示
PythonでYAHOO検索もスクレイピング

前回Google検索結果のTOP100取ったのでYAHOOも追加。 YAHOOは10件ずつしか取れないので注意! #!/usr/bin/env python3 # -*- coding: utf-8 -*- # 指定のURLをブラウザで開く #...

 
 
 

Comments


記事: Blog2_Post

©2021 by 日々Python。Wix.com で作成されました。

bottom of page