zoukankan      html  css  js  c++  java
  • 单线程与多线程的应用 --Python3

    1、单线程应用
    from time import ctime, sleep
    from time import ctime, sleep
    
    
    class ThreadClass:
        def say(self):
            print('Begin say:%s' % ctime())
            sleep(2)
    
        def write(self):
            print('Brgin write:%s' % ctime())
            sleep(2)
    
    
    if __name__ == '__main__':
        t = ThreadClass()
        t.say()
        t.write()
        print('All end:%s' %ctime())
    

    Begin say:Wed Jun 12 17:14:11 2019
    Brgin write:Wed Jun 12 17:14:13 2019
    All end:Wed Jun 12 17:14:15 2019

    2、多线程的应用
    from time import ctime, sleep
    import threading
    
    
    class ThreadClass:
        def say(self, content, loop):
            for i in range(loop):
                print('Say:%s,Time:%s' % (content, ctime()))
                sleep(2)
    
        def write(self, content, loop):
            for i in range(loop):
                print('Write:%s,Time:%s' % (content, ctime()))
                sleep(2)
    
    
    if __name__ == '__main__':
        threadings = []
        tc = ThreadClass()
        t1 = threading.Thread(target=tc.say, args=('say content', 2))
        t2 = threading.Thread(target=tc.write, args=('write content', 2))
        threadings.append(t1)
        threadings.append(t2)
        for t in threadings:
            t.start()
        for t in threadings:
            t.join()
        print('All the end:%s' % ctime())
    

    Say:say content,Time:Wed Jun 12 21:58:31 2019
    Write:write content,Time:Wed Jun 12 21:58:31 2019
    Write:write content,Time:Wed Jun 12 21:58:33 2019
    Say:say content,Time:Wed Jun 12 21:58:33 2019
    All the end:Wed Jun 12 21:58:35 2019

    3、扩展:多进程的应用
    from time import ctime, sleep
    import multiprocessing
    
    
    class ThreadClass:
        def say(self, content, loop):
            for i in range(loop):
                print('Say:%s,Time:%s' % (content, ctime()))
                sleep(2)
    
        def write(self, content, loop):
            for i in range(loop):
                print('Write:%s,Time:%s' % (content, ctime()))
                sleep(2)
    
    
    if __name__ == '__main__':
        multiprocessings = []
        tc = ThreadClass()
        t1 = multiprocessing.Process(target=tc.say, args=('say content', 2))
        t2 = multiprocessing.Process(target=tc.write, args=('write content', 2))
        multiprocessings.append(t1)
        multiprocessings.append(t2)
        for t in multiprocessings:
            t.start()
        for t in multiprocessings:
            t.join()
        print('All the end:%s' % ctime())
    

    Say:say content,Time:Wed Jun 12 22:03:54 2019
    Write:write content,Time:Wed Jun 12 22:03:54 2019
    Say:say content,Time:Wed Jun 12 22:03:56 2019
    Write:write content,Time:Wed Jun 12 22:03:56 2019
    All the end:Wed Jun 12 22:03:58 2019

  • 相关阅读:
    Windows Server2012R2 添加Microsoft .NET Framework 3.5 功能失败的解决方法
    Windows Server2012R2 安装 SharePoint 2013 的必备组件
    pig加载两个不同字段个数的文件?load file with different items(f1有42列,f2有43列读到一个对象中)
    正则表达式的子模式详解
    PHP 递归函数的理解
    仿照美食杰tab选项卡
    tab简单选项卡
    PHP字符串变驼峰方法
    PHP笔试题
    PHP面试题集
  • 原文地址:https://www.cnblogs.com/qikeyishu/p/11013130.html
Copyright © 2011-2022 走看看