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

  • 相关阅读:
    MQTT Server搭建(apache-apollo)和MQtt Client搭建
    playbook 包含多个play
    ansible 批量重启服务
    无法执行 BACKUP LOG,因为当前没有数据库备份
    Microsoft.SqlServer.SmoExtended
    ACTIVEMQ主题、队列设置用户名密码
    Ansible的配置文件:
    MQTT协议之订阅及发布(使用paho-mqtt-client或mqttv3实现)
    采用基于MQTT的ActiveMQ实现消息推送
    ansible Introduction To Ad-Hoc Commands:
  • 原文地址:https://www.cnblogs.com/qikeyishu/p/11013130.html
Copyright © 2011-2022 走看看