[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/We2one/Notes/master/python-basic-notes/python-days/Day_17.md [Back]  [Original]

### 

+  threading  `import threading`

  ```python
  import threading
  
  # thrading.Thread(target=)
  threading.Thread(self, group=None, target=, name=,
                 args=(), kwargs=None, *, daemon=None)
  ```


    1.  ( args=() )    `threading.Thread(target=, args=())`
    
    2. **join()**  : 
  
    3. **setDaemon()**  :  () `.setDaemon(True)`
+ 
        
  + eg: QQ 
        
  4. 
  
      + **getName()** : 
  
      + **setName()** : 
  
        + **is_alive()** :   ( isAlive() )
        
  5. threading 
  
      + **threading.currentThread()  threading.current_thread()** : 
  
      + **threading.enumerate()** :   list (: , )
  
        + **threading.activeCount()  threading.active_count()** : ,  len(threading.enumerate()) 
  
+  (  threading.Thread  run() )

    ```python
    import threading
    import time
    
    class MyThread(threading.Thread):
    
        def __init__(self, num):
            super(MyThread, self).__init__()
            self.num = num
    
        def run(self):
            for i in range(self.num):
                print(f"---run\t{i}---")
    
    if __name__ == "__main__":
        my_thread = MyThread(3)
        my_thread.run()
    ```


+ 

    +  "" 

        + 

        + 
    
+ 

    +  : 
    
###  GIL

+  : (),

    +  ()

    + 

+ threading  Lock 

    + `threading.Lock()` : 

    + `acquire(self, blocking: bool = ..., timeout: float = ...) -> bool` : 

        + blocking  : True  ( True) False 

          ```python
          import threading
          import time
          
          def func1():
              print('t1')
              if t1_lock.acquire():
                  time.sleep(1)
                  print('t1  t1 ')
                  if t2_lock.acquire(blocking=False):
                      print('t1  t2 ')
                      t2_lock.release()
                      print('t1  t2 ')
                  t1_lock.release()
                  print('t1  t1 ')
          
          def func2():
              print('t2')
              if t2_lock.acquire():
                  time.sleep(1)
                  print('t2  t2 ')
                  if t1_lock.acquire(blocking=False):
                      print('t2  t1 ')
                      t1_lock.release()
                      print('t2  t1 ')
                  t2_lock.release()
                  print('t2  t2 ')
          
          if __name__ == "__main__":
              t1_lock = threading.Lock()
              t2_lock = threading.Lock()
          
              t1 = threading.Thread(target=func1)
              t2 = threading.Thread(target=func2)
          
              t1.start()
              t2.start()
          """
          t1
          t2
          t2  t2 t1  t1 
          t2  t2 
          t1  t2 
            
          t1  t2 
          t1  t1 
          """
          ```
          

+ 

  +  acquire()  locked 

  +  blocked  release()  unlocked 

  +  (running) 

    ```python
    import threading
    import time
    
    tickes = 1000
    
    def fun1(window):
        global tickes
        # mutexFlag = mutex.acquire(True)
        # if mutexFlag:
        while tickes > 1:
            # 
            if mutex.acquire():
                tickes -= 1
                time.sleep(0.01)
    
                print(f'{window},{tickes}')
                # 
                mutex.release()
    
    if __name__ == "__main__":
        # 
        mutex = threading.Lock()
    
        t1 = threading.Thread(target=fun1, args=('1', ))
        t2 = threading.Thread(target=fun1, args=("2", ))
    
        t1.start()
        t2.start()
    ```


+ 
  
    + 
    
+ 

    + 

    + 
    
### 

+ 

+ 

+ 

    + 

    +  blocking 

###  ()

+  ()

+ 

### 

+  () `from queue import Queue`

    + python  **Queue ** ()

        + FIFO : 

          + **queue.Queue(self, maxsize=0)**

            ```python
            import queue
            
            # 
            q = queue.Queue()
            q.put(1)
            q.put(2)
            q.put(3)
            i = 0
            while i

Web Proxy Viewer  |  New URL  |  Original Page