Jump to content

Threading Module


JoaoRodrigues

Recommended Posts

Vocês conseguem usar o vosso? Eu tento e dá-me isto:

Time Elapsed: 30.2441420555
-------------------------

Now with MT...
Traceback (most recent call last):
  File "threading.py", line 1, in <module>
    import threading, os, time
  File "/home/joao/Desktop/threading.py", line 19, in <module>
    class myThread(threading.Thread):
AttributeError: 'module' object has no attribute 'Thread'

O código em Python que tento executar é:

import threading, os, time

print 'I will ping a dozen sites...'
print '\n\nWithout MT...'
time_start = time.time()

site_list = ['www.google.com', 'www.yahoo.com', 'www.msn.com', 'www.pubmed.org', 'www.portugal-a-programar.org', 'woc.uc.pt/bioquimica']

for i in site_list:
command = 'ping -c 3 '+i
os.system(str(command))

time_finish = time.time()

print 'Time Elapsed:',time_finish-time_start

print '-'*25+'\n\nNow with MT...'

class myThread(threading.Thread):
def __init__(self, command):
	Thread.__init__(self)
	self.command = command
def run(self):
	os.system(str(command))

for i in site_list:
time_start = time.time()
command = 'ping -c 3 '+i
current = myThread(command)
current.start()
time_finish = time.time()
print 'Time Elapsed:',time_finish-time_start
Link to comment
Share on other sites

Não tou muito por dentro do python, mas ...

ao fazeres import ele apenas importa o modulo por isso para te referires a objectos desse modulo tens que usar a sintaxe modulo.objecto.funcao

se fizeres from modeulo import * ele ja te importa o modulo e os objectos la contidos

tenta com  threading.Thread.__init__()

Link to comment
Share on other sites

Now with MT...

Traceback (most recent call last):

  File "threading.py", line 1, in <module>

    import threading, os, time

  File "/home/joao/Desktop/threading.py", line 19, in <module>

    class myThread(threading.Thread):

AttributeError: 'module' object has no attribute 'Thread'

Como podes ver pedrotuga, ele está a incluir o próprio ficheiro. Ele pode experimentar trocar o nome do ficheiro e voltar a tentar. 😄

Não peças ajuda por PM! A tua dúvida vai ter menos atenção do que se for postada na secção correcta do fórum!

Link to comment
Share on other sites

Fiz e funciona.. já posto o código e o output

Now with MT...
ping -c 3 www.google.com
ping -c 3 www.yahoo.com
ping -c 3 www.msn.com
ping -c 3 www.pubmed.org
ping -c 3 www.portugal-a-programar.org
ping -c 3 woc.uc.pt/bioquimica
Time Elapsed: 0.000221014022827
ping: unknown host woc.uc.pt/bioquimica
Yo
ping: unknown host woc.uc.pt/bioquimica
Yo
ping: unknown host woc.uc.pt/bioquimica
Yo
ping: unknown host woc.uc.pt/bioquimica
Yo
ping: unknown host woc.uc.pt/bioquimica
Yo
ping: unknown host woc.uc.pt/bioquimica
Yo

 """Continua de cima"""
class myThread(Thread):
def __init__(self, command):
	Thread.__init__(self)
	self.command = command
	print self.command
def run(self):
	os.system(str(command))
	print 'Yo'

for i in site_list:
time_start = time.time()
command = 'ping -c 3 '+i
current = myThread(command)
current.start()
time_finish = time.time()
print 'Time Elapsed:',time_finish-time_start
Link to comment
Share on other sites

djthyrax... não percebi o que quiseste dizer dois posts acima.

Ora ve lá o link que eu afixei.

Ele não está a usar sintaxe de acesso a objectos consistente.

tipo...

import threading

threading.Thread() <---- isto pode-se usar

Thread() <--- isto não

from threading import thread
Thread() <--- isto já se pose usar

Moral da história, no código dele, o objecto 'threading' não estava definido.

class myThread(threading.Thread):
def __init__(self, command):
	Thread.__init__(self) #<---aqui tem que ser threading.Thread.__init__(self)
Link to comment
Share on other sites

djthyrax... não percebi o que quiseste dizer dois posts acima.

O problema estava na importação do modulo treading, que  está num ficheiro chamado treading.py algures na pasta do site-packages (ou algo parecido)

O python importa os módulos procurando na path por um ficheiro com esse nome, essa path é um array em que a primeira posição tem a path de onde o programa se encontra e nas restantes as path's do systema e das classes do python (um pouco como a classpath do java, um print sys.path e entendes logo o que tou a dizer).

Como o ficheiro dele tem o mesmo nome do módulo e a path de onde  programa se encontra na primeira posição ele vai importar o próprio ficheiro em vez de importar o módulo pois encontrou um treading.py e não vai procurar mais

É esta a razão do objecto treading não estar definido pois nunca chegou a ser importado

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.