简体   繁体   English

Python多线程有限线程的for循环

[英]Python Multithreading a for loop with limited Threads

i am just learning Python and dont have much expierence with Multithreading. 我只是在学习Python,对Multithreading的了解也不多。 I am trying to send some json via the Requests session.post Method. 我正在尝试通过Requests session.post方法发送一些json。 This is called in the function at the bottem of the many for loops i need to run through the dictionary. 我需要遍历字典的许多for循环底部的函数中调用此函数。

Is there a way to let this run in paralell? 有没有办法让它并行运行?

I also have to limit my numbers of Threads, otherwise the post calls get blocked because they are to fast after each other. 我还必须限制线程的数量,否则发布调用将被阻塞,因为它们彼此之间会很快。 Help would be much 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'])

First, structuring your code differently might improve the speed without the added complexity of threading. 首先,在不增加线程复杂性的情况下,以不同的方式构造代码可以提高速度。

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'])

To start with, we will look up equipment['itemHash'] and equipment['characterIndex'] only once. 首先,我们将只查找一次equipment['itemHash']equipment['characterIndex']

Instead of explicitly looping over RefHashList , you could use the in operator. 可以使用in运算符来代替显式循环RefHashList This moves the loop into the Python virtual machine, which is faster. 这会将循环移至速度更快的Python虚拟机中。

And instead of a nested if -conditional, you could use a single conditional using and . 除了嵌套的if -condition之外,您还可以使用and来使用单个条件。

Note: I have removed the unused parameter List , and replaced it with res . 注意:我已经删除了未使用的参数List ,并将其替换为res It is generally good practice to write functions that only act on parameters that they are given, not global variables. 通常最好的做法是编写仅作用于给定参数而不是全局变量的函数。

Second, how much extra performance do you need? 其次,您需要多少额外的性能? How much time is there on average between the SendJsonViaSession calls, and how small can this this time become before calls get blocked? SendJsonViaSession调用之间平均要花费多少时间,并且这次可以变成多少时间才能阻止调用? If the difference between those numbers is small, it is probably not worth to implement a threaded sender. 如果这些数字之间的差很小,则可能不值得实现线程发件人。

Third, a design feature of the standard Python implementation is that only one thread at a time can be executing Python bytecode. 第三,标准Python实现的设计功能是一次只能执行一个线程执行Python字节码。 So it is not certain that threading will improve performance. 因此,不确定线程​​可以提高性能。

Edit: 编辑:

There are several ways to run stuff in parallel in Python. 有几种方法可以在Python中并行运行内容。 There is multiprocessing.Pool which uses processes, and multiprocessing.dummy.ThreadPool which uses threads. multiprocessing.Pool它使用过程,并multiprocessing.dummy.ThreadPool它使用线程。 And from Python 3.2 onwards there is concurrent.futures , which can use processes or threads. 从Python 3.2开始,存在concurrent.futures ,可以使用进程或线程。

The thing is, neither of them has rate limiting. 问题是,它们都没有速率限制。 So you could get blocked for making too many calls. 因此,您可能因拨打太多电话而被阻止。 Every time you call SendJsonViaSession you'd have to save the current time somehow so that all processes or threads can use it. 每次调用SendJsonViaSession ,都必须以某种方式保存当前时间,以便所有进程或线程都可以使用它。 And before every call, you would have to read that time and wait if it is too close to the last call. 在每次通话之前,您都必须阅读该时间,然后等待距离最近的通话时间太近。

Edit2: 编辑2:

If a call to SendJsonViaSession only takes 0.3 seconds, you should be able to do 3 calls/second sequentially. 如果对SendJsonViaSession的调用仅花费0.3秒,则您应该能够连续每秒进行3次调用。 But your code only does 1 call/second. 但是您的代码每秒只进行1次调用。 This implies that the speed restriction is somewhere else. 这意味着速度限制在其他地方。 You'd have to profile your code to see where the problem lies. 您必须分析您的代码以查看问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM