Uruchamianie metod klas w wątkach (python)
Obecnie uczę się Pythona i klas i mam podstawowe pytanie, ale nie znalazłem na nie odpowiedzi. Powiedzmy, że mam tę klasę dummy
class DomainOperations:
def __init__(self, domain):
self.domain = domain
self.domain_ip = ''
self.website_thumbnail = ''
def resolve_domain(self):
#resolve domain to ipv4 and save to self.domain_ip
def generate_website_thumbnail(self):
#generate website thumbnail and save the url to self.website_thumbnail
Chcę uruchomić jednocześnie resolve_domain i generate_website_thumbnail i po zakończeniu wątków chcę wydrukować IP i miniaturkę.
EDIT: wiem, że powinienem używać wątków, może coś takiego
r = DomainOperations('google.com')
t1 = threading.Thread(target=r.resolve_domain)
t1.start()
t2 = threading.Thread(target=r.generate_website_thumbnail)
t2.start()
Ale czy powinienem używać ich poza klasą? Czy powinienem napisać kolejną klasę do obsługiwać nitki?
Jaki jest właściwy sposób aby to zrobić?
36
Author: nacholibre, 2013-03-12
2 answers
Jeśli wywołasz je z klasy, jest to tak proste jak:
import threading
class DomainOperations:
def __init__(self):
self.domain_ip = ''
self.website_thumbnail = ''
def resolve_domain(self):
self.domain_ip = 'foo'
def generate_website_thumbnail(self):
self.website_thumbnail= 'bar'
def run(self):
t1 = threading.Thread(target=self.resolve_domain)
t2 = threading.Thread(target=self.generate_website_thumbnail)
t1.start()
t2.start()
t1.join()
t2.join()
print(self.domain_ip, self.website_thumbnail)
if __name__ == '__main__':
d = DomainOperations()
d.run()
61
Author: A. Rodas,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-07-04 22:24:37
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-07-04 22:24:37
Możesz dziedziczyć klasę wątku w DomainOperation, w ten sposób kod będzie bardziej czysty i zrozumiały. musisz nadpisać metodę run () .
from threading import threading.Thread
class DomainOperations(Thread):
def __init__(self):
self.domain_ip = ''
self.website_thumbnail = ''
def resolve_domain(self):
self.domain_ip = 'foo'
def generate_website_thumbnail(self):
self.website_thumbnail= 'bar'
def run(self):
#domain will be resolved on first thread
self.resolve_domain()
#thumbnail will be resolved on second OR newly created below thread
thread2 = Thread(target=self.generate_website_thumbnail)
thread.start()
# thread1 will wait for thread2
self.join()
# thread2 will wait for thread1, if it's late.
thread2.join()
# here it will print ip and thumbnail before exiting first thread
print(self.domain_ip, self.website_thumbnail)
I w ten sposób rozpoczniesz swoje wątki.
if __name__ == '__main__':
thread1 = DomainOperations()
thread1.start()
1
Author: Hafiz Hashim,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-06 19:12:02
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-06 19:12:02