Python Multi-threaded for loop with limited threads

I am just learning Python and don't have much experience with Multithreading. I am trying to send json via session.post method of requests. This is called in the function at the bottom of the many loops that need to be run through the dictionary.

Is there any way to enable this run in paralell?

I also need to limit the number of threads, otherwise post-calls will be blocked, because they will be fast one after another. Help would be greatly appreciated.

def doWork(session, List, RefHashList):
    for itemRefHash in RefHashList:
        for equipment in res['Response']['data']['items']:
            if equipment['itemHash'] == itemRefHash:
                if equipment['characterIndex'] != 0:
                    SendJsonViaSession(session, getCharacterIdFromIndex(res, equipment['characterIndex']), itemRefHash, equipment['quantity'])
+4
source share
1 answer

First, structuring your code in different ways can increase speed without the added complexity of streaming.

def doWork(session, res, RefHashList):
    for equipment in res['Response']['data']['items']:
        i = equipment['itemHash']
        k = equipment['characterIndex']
        if i in RefHashList and k != 0:
            SendJsonViaSession(session, getCharacterIdFromIndex(res, k), i, equipment['quantity'])

equipment['itemHash'] equipment['characterIndex'] .

RefHashList in. Python, .

if -conditional, and.

. List res. , , , , .

-, ? SendJsonViaSession , ? , , .

-, Python , - Python. , .

Edit:

Python . multiprocessing.Pool, , multiprocessing.dummy.ThreadPool, . Python 3.2 concurrent.futures, .

, . , . , SendJsonViaSession, , . , .

Edit2:

SendJsonViaSession 0,3 , 3 / . 1 /. , - . profile , , .

+3

Source: https://habr.com/ru/post/1668043/


All Articles