0

I open more Process with python to retrive files via urlib2, I only put the call to the fucntion.

t = Process(target=traer, args=(dir, listado[contador],))

This is the function that call in the process object

def traer(dir,y):
    global listado2
    try:
        file = urllib2.urlopen(dir+y)
        nombre=y
        output = open('C:/ndfd/degrib/bin/archivos/'+nombre ,'wb')
        output.write(file.read())
        output.close()
    except urllib2.HTTPError, e:
        print 'HTTPError = ' + str(e.code)
    except urllib2.URLError, e:
        listado2.append(y)
        print 'URLError = ' + str(e.reason)

This is in root of the archive

 if __name__ == '__main__':
     global listado
     global listado2
     listado=[]
     listado2=[]
     crear_trip(dir,listado)
     salida_bath(dir,listado,listado2)

inside salida_bath are inside a loop while that turn on the process.

In the exception I will like append to a global variable called listado2 but this not work, say me that it is undefined listado2.

MY FIX OF THIS PROBLEM FOR ME, change the strategy

create inside the salida_bath

    manager = Manager()
    listado2 = manager.dict()

put a contador of the process and past the new listado2 to the process

    t = Process(target=traer, args=(contador,dir, listado[contador],listado2))

and in traer function change this.

    except urllib2.URLError, e:
        listado2[contador]=y
        print 'URLError = ' + str(e.reason)

check outside that this is working

 for x in listado2.values():
      listado.append(x)
 print listado

1 Answer 1

3

The global statement only informs the parser that the variable is not to be considered local, but it does not define a new variable if one does not exist. You're still responsible to initialize it somewhere before accessing it. You seem to assume it is a list (listado2.append(y)), but nowhere in your code you actually initialize it with a list value. How would Python know which .append() method to invoke on a variable that has never been assigned anything?

Sign up to request clarification or add additional context in comments.

3 Comments

Hello, I initialize before call the process but outside. The process is calling in another function that initialize the global vars
Then you should edit your question and include your code that shows how, where and when you initialize the listado2 variable.
Thank you for your time. I resolved, I dont know if it is bad or well, but this is working. ;) sorry myd bad english.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.