美文网首页Py100Skills
[Py030] 同时执行两个函数

[Py030] 同时执行两个函数

作者: 安哥生个信 | 来源:发表于2019-08-20 10:49 被阅读4次
    from threading import Thread
    import time
    import datetime
    
    def func1():
        print(f'{datetime.datetime.now()}  func1 starting')
        time.sleep(20)
        print(f'{datetime.datetime.now()}  func1 working')
        time.sleep(20)
        print(f'{datetime.datetime.now()}  func1 ending')
    
    def func2():
        print(f'{datetime.datetime.now()}  func2 starting')
        time.sleep(20)
        print(f'{datetime.datetime.now()}  func2 working')
        time.sleep(20)
        print(f'{datetime.datetime.now()}  func2 ending')
        
    Thread(target=func1).start()
    Thread(target=func2).start()
    

    返回结果

    2019-08-20 10:45:59.839427  func1 starting
    2019-08-20 10:45:59.839821  func2 starting
    2019-08-20 10:46:19.852530  func1 working
    2019-08-20 10:46:19.859896  func2 working
    2019-08-20 10:46:39.853510  func1 ending
    2019-08-20 10:46:39.879981  func2 ending
    

    参考https://codeday.me/bug/20180602/172668.html

    相关文章

      网友评论

        本文标题:[Py030] 同时执行两个函数

        本文链接:https://www.haomeiwen.com/subject/mapusctx.html