![](/img/trans.png)
[英]Why do I get NameError: name 'as_completed' is not defined (I have from concurrent.futures import ThreadPoolExecutor)
[英]First time attempting to thread using concurrent.futures-Why do I get no output?
我是python的新手,所以我的代码可能存在多处错误。 但是我无法调试,因为我实际上没有任何输出,包括任何错误。 希望有帮助。 我在python 3.5中。 干杯!
print('starting')
def simple_function(comic_start, comic_end):
for urlNumber in range(comic_start, comic_end):
print('Downloading page http://xkcd.com/%s...' % (urlNumber))
driver = selenium.webdriver.PhantomJS()
driver.get('http://xkcd.com/%s' % (urlNumber))
print('finding page')
form= driver.title
print(driver.title)
driver.quit()
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
executor.map(simple_function, (1, 100))
输出:
runfile('/Users/Seansmac/Desktop/Python/my_classroom/threading_training/concurrent.f1.py', wdir='/Users/Seansmac/Desktop/Python')
starting
编辑:这是与此类似的问, Concurrent.futures使用指南-一个同时使用线程和处理的简单示例 ,但是就像OP所说的那样,答案太复杂了!
您正在将具有两个值的元组映射到该函数。 在每个映射上,它只发送一个参数,但是您的函数需要两个论点comic_start
和comic_end
要看到这一点,您可以排除其他争论并执行以下操作:
def simple_function(first_arg):
print(first_arg)
with cf.ThreadPoolExecutor(max_workers=10) as executor:
executor.map(simple_function, (1, 100))
结果:
1
100
修改后的代码:
import concurrent.futures as cf
def simple_function(_range):
#fill with driver stuff etc
for num in _range:
print('Downloading page http://xkcd.com/%s...' % (num))
_max = 200 #max range you want
workers = 10
with cf.ThreadPoolExecutor(max_workers=workers) as e:
cs= _max // workers #chunksize to split across threads
#we chunk the range to be split across the threads so you can iterate
#over the chunked range in the mapped function.
#x + cs if x + cs <= _max else _max next upper bound by chunksize but
#we don't want to exceed the maximum range you want, so we default to _max
#if the next upper bound exceeds this
#x - 1 if x!= 0 else x, start initial range at 0, and last upper
#bound is exclusive so x - 1 if were not at the initial range.
ranges = [range(x - 1 if x != 0 else x, x + cs if x + cs <= _max else _max)
for x in range(0, _max, cs)]
e.map(simple_function, ranges)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.