GreenPool报TypeError: 'NoneType' object is not callable

发表于 2018-04-26   |   分类于 技术

今天在使用greenpool的时候,报出了一个莫名奇妙的错误,百思不得其解。

现场的代码大概是如下这个样子:

def func(arg1):
    print "test func"

pool = eventlet.GreenPool(4)
pool.spawn_n(func(arg))

不知道大家有没有看出什么问题。执行的时候会报如下的错误,但是相应的程序确实执行了,感到很奇怪。

错误信息如下:

Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/eventlet/greenpool.py", line 82, in _spawn_n_impl
    func(*args, **kwargs)
TypeError: 'NoneType' object is not callable
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/eventlet/greenpool.py", line 82, in _spawn_n_impl
    func(*args, **kwargs)
TypeError: 'NoneType' object is not callable
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/eventlet/greenpool.py", line 82, in _spawn_n_impl
    func(*args, **kwargs)
TypeError: 'NoneType' object is not callable
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/eventlet/greenpool.py", line 82, in _spawn_n_impl
    func(*args, **kwargs)
TypeError: 'NoneType' object is not callable
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/eventlet/greenpool.py", line 82, in _spawn_n_impl
    func(*args, **kwargs)
TypeError: 'NoneType' object is not callable

查看文档发现了问题所在:
spawn_n的正确调用方法应该是:

spawn_n(function, *args, **kwargs)

第一个参数是方法这个对象,而该方法需要的参数作为第二第三个参数传进去的。
正确的应该是:

def func(arg1):
    print "test func"

pool = eventlet.GreenPool(4)
pool.spawn_n(func, arg) # 错误 :pool.spawn_n(func(arg))

如果第一个参数传入func(arg),实际上传给spawn_n的第一个参数是func(arg)的返回值,greenPool再创建绿色线程去执行这个返回值,如果这个返回值是None,就会报上面的错误。

发表新评论

© 2017 Powered by Typecho
苏ICP备15035969号-3