Bottom Page

Thread Rating:
  • 3 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with threads in for loop
#1
Hello Friends,

I am using a nested loop first loop iterate for 50,000 times and second loop iterate for 500 times if i use normal approach
i.e.
for i in range(0,50000):
    for j in range(0,250)
        print(i,j)
It is taking huge time so can any one tell me how can i execute this nested for loop faster? can you please share the code as well

i am using this code but not very much fast.


import time

from threading import Thread

class GetMyThread(Thread):
    def __init__(self, start, end, start2, end2):
        self.ix = start 
        self.iy= end
        self.jx = start2
        self.jy = end2
        super(GetMyThread, self).__init__()
        for i in range(self.ix, self.iy):
            for j in range(0, len(overallKeywordsFrequency)):
                    if(overallKeywordsFrequency[j] in (df.iloc[i]['Keywords'].lower().strip())):
                        matrix[i,j] = 1
       
        
        
                

def get_responses():
    start = time.time()
    threads = []
    num_splits = 50
    split_size = len(df)//num_splits
    split_size2 = len(overallKeywordsFrequency)//num_splits
    for i in  range(num_splits):
        start = i * split_size
        start2 = i * split_size2
        end = len(df) if i+1 == num_splits else (i+1) * split_size
        end2 = len(overallKeywordsFrequency) if i+1 == num_splits else (i+1) * split_size2
        t = GetMyThread(start, end, start2, end2)
        threads.append(t)
        t.start()
    for t in threads:
        t.join()

get_responses()
buran wrote Jan-11-2019, 12:50 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Quote
#2
I have the suspicion that your code is taking an exceedingly long time due to a bug. The GetMyThread.__init__() method is running the matrix loop. Properly, that loop should be in the GetMyThread.run() method instead. I say this because you later call the thread.join() method and that method listens for the termination of the thread.run() method according to the documentation. If the thread.run() method isn't being used, thread.join() should not work. In turn, that means that you have a thread running and the program isn't stopping to collect its data.

Also, I don't see any mechanism for collecting the data from the thread.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  loop through list or double loop 3Pinter 4 184 Dec-05-2018, 06:17 AM
Last Post: 3Pinter
  stop multiple threads jeuvrey 5 264 Nov-15-2018, 01:34 PM
Last Post: jeuvrey
  Handling Many Threads. MuntyScruntfundle 3 211 Oct-17-2018, 05:07 PM
Last Post: stranac
  Quitting multiple threads MuntyScruntfundle 3 198 Oct-17-2018, 05:14 AM
Last Post: volcano63
  Threads in threads? MuntyScruntfundle 1 196 Oct-14-2018, 09:10 AM
Last Post: Gribouillis
  starting multi threads MuntyScruntfundle 7 346 Oct-11-2018, 08:52 PM
Last Post: nilamo
  while loop issue - stuck in the loop! EricMichel 6 461 Aug-06-2018, 03:59 PM
Last Post: EricMichel
  problem to use multi-threads in a code for telnet sessions anna 6 729 Jun-27-2018, 10:02 AM
Last Post: anna
  Using Terminating Signal to Terminate Long Threads crcali 1 397 Apr-06-2018, 01:26 AM
Last Post: woooee
  Output not printing when execute scripts in threads anna 1 433 Mar-21-2018, 05:08 PM
Last Post: woooee

Forum Jump:


Users browsing this thread: 1 Guest(s)