• 进程和线程


    [线程]:操作系统进行运算调度的最小单位,它被包含在进程之中,是进程的实际运作单位,一个线程是进程中一个单一的顺序控制流,一个进程可以并发多个线程,每个线程可以执行不同的任务

    [进程]:执行一个程序的实例叫进程

    ---------------------------------------------------------------------------------------------

    线程的有两种调用方式:1.直接调用 2.继承调用

    直接调用[例]:

     1 import threading
     2 import time
     3 def hello(num):
     4 
     5   print('hello %d' %num)
     6 
     7   time.sleep(3)
     8 
     9 
    10 if __name__='__main__':
    11     t1=threading.Thread(target=hello,args=(1,))  #生成线程实例t1
    12     t2=threading.Thread(target=hello,args=(2,))  #生成线程实例t2
    13     t1.start()    #启动线程t1
    14     t2.start()    #启动线程t2

    继承调用[例]:

     1 import threading
     2 import time
     3 
     4 class MyThread(threading.Thread):    
     5     
     6     def __init__(self,num):
     7         threading.Thread.__init__(self)
     8         self.num=num
     9     
    10      def hello(self):
    11          print('hello %s'%self.num)
    12          time.sleep(3)
    13 
    14 if __name__=='__main__':
    15     t1=MyThread('1')
    16     t2=MyThread('2')
    17     t1.start()
    18     t2.start()

    lock锁:
    GIL锁处于编译层的锁;

    threading.Lock(),threading.RLock()处理python层的锁,保护自己内存数据的锁;

                                       ^__递归锁

  • 相关阅读:
    RPC
    Memcache
    python supervisor使用
    代码规范
    值传递,引用传递
    日志文件目录
    input标签的事件汇总
    箭头函数
    JS数组reduce()方法
    关于Python变量的学习总结
  • 原文地址:https://www.cnblogs.com/oleli/p/5326266.html
Copyright © 2020-2023  润新知