Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:确定要生成的线程数_Python_Multithreading_Sockets - Fatal编程技术网

Python:确定要生成的线程数

Python:确定要生成的线程数,python,multithreading,sockets,Python,Multithreading,Sockets,IOError:[Errno socket error][Errno 10055]无法对套接字执行操作,因为系统缺少足够的缓冲区空间或队列已满 为了防止程序遇到上述错误,我应该如何确定线程实例的数量 def do_work(sym): if quote.get_price(i) != '0.00': print '%s: %s' % (i, squote.get_price(i)) for i in perm: t = Thread(target=do_work

IOError:[Errno socket error][Errno 10055]无法对套接字执行操作,因为系统缺少足够的缓冲区空间或队列已满

为了防止程序遇到上述错误,我应该如何确定线程实例的数量

def do_work(sym):
    if quote.get_price(i) != '0.00':
        print '%s: %s' % (i, squote.get_price(i))

for i in perm:
    t = Thread(target=do_work, args=(i,))
    t.start()
注: 1.lenperm大约有27000人 2.quote.get_价格的作用类似于:

return urllib.urlopen(url).read().strip().strip('"')

27000个线程的开销可能是导致问题的原因。尝试创建一个你想要接收报价的股票队列,并使用5-10个线程的工作池来检索价格。

27000个线程的开销可能是导致问题的原因。尝试创建一个您希望接收报价的股票队列,并使用5-10个线程的工作池来检索价格。

使用线程池而不是创建27000个线程

import multiprocessing.pool

def do_work(sym):
    if quote.get_price(sym) != '0.00':
        print '%s: %s' % (sym, squote.get_price(sym))

if __name__ == '__main__':
    number_of_threads = 4
    pool = multiprocessing.pool.ThreadPool(number_of_threads)
    pool.map(do_work, perm)

使用线程池,而不是创建27000个线程

import multiprocessing.pool

def do_work(sym):
    if quote.get_price(sym) != '0.00':
        print '%s: %s' % (sym, squote.get_price(sym))

if __name__ == '__main__':
    number_of_threads = 4
    pool = multiprocessing.pool.ThreadPool(number_of_threads)
    pool.map(do_work, perm)


为什么你一开始就产生27000个线程?你真的想并行阅读27000个网页吗?即使生成了线程,这听起来也有问题。@exantas这个问题的重点不是我想生成27000个线程。它是关于尝试确定要生成的线程的最佳数量。我如何确定你为什么首先要产生27000个线程?你真的想并行阅读27000个网页吗?即使生成了线程,这听起来也有问题。@exantas这个问题的重点不是我想生成27000个线程。它是关于尝试确定要生成的线程的最佳数量。我如何确定数字5-10是从哪里来的?为什么会有这样的范围?@antz这是基于经验。这可能是错误的。如果效率对你来说很重要,你会想做个实验。你能详细说明一下如何进行这个实验吗?它只是想出一个数字,并将它的表现与其他数字进行比较吗?数字5-10是从哪里来的?为什么会有这样的范围?@antz这是基于经验。这可能是错误的。如果效率对你来说很重要,你会想做个实验。你能详细说明一下如何进行这个实验吗?它只是拿出一个数字来比较它的表现吗?为什么是4?为什么不是3、5、6、7、8、9等等?@antz,4是我机器的核心数。您需要调查哪个数字最适合您的任务。@antz,如果您不将任何内容传递给multiprocessing.pool.ThreadPool,它将默认为核心数。啊,我明白了。这是有道理的。我得到这个错误。pool=multiprocessing.pool.ThreadPoolnumber\u of_threads AttributeError:“module”对象没有属性“pool”。我有python 2.7。3@antz,请确保导入multiprocessing.pool,而不是multiprocessing.why 4?为什么不是3、5、6、7、8、9等等?@antz,4是我机器的核心数。您需要调查哪个数字最适合您的任务。@antz,如果您不将任何内容传递给multiprocessing.pool.ThreadPool,它将默认为核心数。啊,我明白了。这是有道理的。我得到这个错误。pool=multiprocessing.pool.ThreadPoolnumber\u of_threads AttributeError:“module”对象没有属性“pool”。我有python 2.7。3@antz,请确保导入multiprocessing.pool,而不是multiprocessing。