关于python:在赋值之前引用的局部变量’sresult’

local variable 'sresult' referenced before assignment

尝试使用pp时遇到了多个问题。我运行的是python2.6和pp 1.6.0 rc3。使用以下测试代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
import pp
nodes=('mosura02','mosura03','mosura04','mosura05','mosura06',
       'mosura09','mosura10','mosura11','mosura12')

def pptester():
        js=pp.Server(ppservers=nodes)
        tmp=[]
        for i in range(200):
                tmp.append(js.submit(ppworktest,(),(),('os',)))
        return tmp

def ppworktest():
        return os.system("uname -a")

给出以下结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
In [10]: Exception in thread run_local:
Traceback (most recent call last):
  File"/usr/lib64/python2.6/threading.py", line 525, in __bootstrap_inner
    self.run()
  File"/usr/lib64/python2.6/threading.py", line 477, in run
    self.__target(*self.__args, **self.__kwargs)
  File"/home/wkerzend/python_coala/lib/python2.6/site-packages/pp.py", line 751, in _run_local
    job.finalize(sresult)
UnboundLocalError: local variable 'sresult' referenced before assignment

Exception in thread run_local:
Traceback (most recent call last):
  File"/usr/lib64/python2.6/threading.py", line 525, in __bootstrap_inner
    self.run()
  File"/usr/lib64/python2.6/threading.py", line 477, in run
    self.__target(*self.__args, **self.__kwargs)
  File"/home/wkerzend/python_coala/lib/python2.6/site-packages/pp.py", line 751, in _run_local
    job.finalize(sresult)
UnboundLocalError: local variable 'sresult' referenced before assignment

Exception in thread run_local:
Traceback (most recent call last):
  File"/usr/lib64/python2.6/threading.py", line 525, in __bootstrap_inner
    self.run()
  File"/usr/lib64/python2.6/threading.py", line 477, in run
    self.__target(*self.__args, **self.__kwargs)
  File"/home/wkerzend/python_coala/lib/python2.6/site-packages/pp.py", line 751, in _run_local
    job.finalize(sresult)
UnboundLocalError: local variable 'sresult' referenced before assignment

Exception in thread run_local:
Traceback (most recent call last):
  File"/usr/lib64/python2.6/threading.py", line 525, in __bootstrap_inner
    self.run()
  File"/usr/lib64/python2.6/threading.py", line 477, in run
    self.__target(*self.__args, **self.__kwargs)
  File"/home/wkerzend/python_coala/lib/python2.6/site-packages/pp.py", line 751, in _run_local
    job.finalize(sresult)
UnboundLocalError: local variable 'sresult' referenced before assignment

非常感谢您的帮助。


我无法读取您的代码,因为它的格式不正确,但我可以告诉您您的确切问题:您试图从函数内部修改一个名为"sresult"的全局变量,但您没有将此行添加到函数的开头:

global sresult

如果不声明变量global,那么如果您试图在函数中分配它,python会假定它是函数的局部变量,因此当您试图修改或访问它时,python会抱怨您还没有"绑定局部变量"(也就是说,分配它或给它一个值)。


这是PP库中的一个bug。修复它,或者等待它被修复。