Home > other >  Multi-threaded tasks never end, just stuck in it, a great god, please help me have a look
Multi-threaded tasks never end, just stuck in it, a great god, please help me have a look

Time:10-07

# coding: utf-8
The import requests
The import threading
From the queue the import queue
The import time
Num_xian=input (" please enter the number of threads to perform:)
# add thread
ThreadList=int (num_xian)
# set the queue length
WorkQueue=Queue (10)
# thread pool
Threads=[]
Stare=time. Time ()

Def crawler (threadName, q) :
# implement multithreaded
# from the queue data
J=q.g et (timeout=2)
Try:
Print (' thread: + threadName)
Except the Exception as e:
Print (q.q size (), threadName "thread running error", e)

Class myThread (threading. Thread) :
Def __init__ (self, name, q) :
Threading. Thread. __init__ (self)
The self. The name=name
Self. Q=q
Def run (self) :
Try:
Thread start print (self. The name + ", ")
Crawler (self. The name, the self. Q)
Print (self. Name + number "thread ends")
Except:
Print (self. The name + "thread start failure")

# to create a new thread
For tName in range (1, threadList + 1) :
Thread=myThread (tName, workQueue)
Thread. The start ()
Threads. Append (thread)

# read data into the queue
Filename='saas_user_mini. TXT'
F=open (filename, the 'r', encoding="utf-8"). The readlines ()

For I in f:
WorkQueue. Put (I)
# all threads waiting for complete
For t in threads:
T.j oin ()

End=time. Time ()
Print (' Queue multithreaded batch execution time is: ', end - start)

CodePudding user response:

For bosses to help
  • Related