zoukankan      html  css  js  c++  java
  • 网络编程之多线程——开启多线程的两种方式

    网络编程之多线程——开启多线程的两种方式

    一、threading模块

    multiprocess模块完全模仿了threading模块的接口,二者在使用层面,有很大的相似性,因而不再详细介绍了。

    二、开启线程的两种方式

    方式一

    from threading import Thread
    import time
    def sayhi(name):
        time.sleep(2)
        print('%s say hello' %name)
    if __name__ == '__main__':
        t=Thread(target=sayhi,args=('egon',))
        t.start()
        print('主线程')
    

    方式二

    from threading import Thread
    import time
    class Sayhi(Thread):
        def __init__(self,name):
            super().__init__()
            self.name=name
        def run(self):
            time.sleep(2)
            print('%s say hello' % self.name)
    if __name__ == '__main__':
        t = Sayhi('egon')
        t.start()
        print('主线程')
    
  • 相关阅读:
    Spring中的一些常用接口
    ApplicationContextAware的作用
    用spring的 InitializingBean 的 afterPropertiesSet 来初始化
    虚拟机扩容(/dev/mapper/centos-root 空间不足)
    AJAX
    Git
    jQuery
    JS
    JS
    jQuery
  • 原文地址:https://www.cnblogs.com/Kwan-C/p/11589383.html
Copyright © 2011-2022 走看看