Mr. AMI
Mr. AMI

Reputation: 17

How can use threading in for loop in python

I want to use multitasking in a while loop and a for loop in python to make the code faster, I have 300 coins in 50 lists and all the lists in one list.

while True:
     for i in range(len(usdtLists)):
         Thread(target= stopping_volume, args= (usdtLists[i], i)).start()

but I always get this error: unsupported operand type(s) for -: 'NoneType' and 'relativedelta'

Upvotes: 0

Views: 197

Answers (1)

Louis Lac
Louis Lac

Reputation: 6436

Launching that many threads in parallel may be inefficient and cause errors. You should create a ThreadPoolExecutor (or ProcessPoolExecutor) and submit work to it. For instance you can use the .map(...) method to execute the same function with different arguments from an iterator:

from concurrent.futures import ThreadPoolExecutor

with ThreadPoolExecutor() as e:
  e.map(stopping_volume, usdList, range(len(usdList)))

Upvotes: 1

Related Questions