05
2021
08

multithreading + queue

import threading
import time
from queue import Queue
from google.colab import drive
from google.colab import files
from google.colab import auth
auth.authenticate_user()
from oauth2client.client import GoogleCredentials
drive.mount('/content/drive/')

def producer():
    f = open('/content/drive/My Drive/TS/s.txt','r')
    for line in f.readlines():
        line=line.replace('file ','/content/drive/My Drive/TS/').strip()
        qq.put(line)
        print("Adding_Lines_To_Pool:", line, "No. of value in Pool:",qq.qsize())
        f.close()

        

def consumer(args):
    while not qq.empty():
        time.sleep(1)
        line = qq.get()
        print("Threading:", args,'READING_Value_From_Pool:', line)
        if qq.empty():
          print("all read~")
          
        


threads = []
for i in range(2,100):
  t=threading.Thread(target=consumer, args=(str(i), ))
  threads.append(t)
#Th2 = threading.Thread(target=get_id, args=(2, ))

if __name__ == "__main__":
  qq = Queue()
  time.sleep(0)
  Th1 = threading.Thread(target=producer, )
  Th1.start()
  #Th2.start()

  for i in threads:
    i.start()

  Th1.join()
  for i in threads:
    i.join()


« 上一篇 下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。