Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Class methods in threads (python)

I'm currently learning Python and Classes and I have a basic question, but I didn't find any answer to it. Let's say I have this dummy class

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 

I want to run simultaneously resolve_domain and generate_website_thumbnail and when the threads are finished I want to print the IP and the thumbnail.

EDIT: I know I should use threads, maybe something like this

r = DomainOperations('google.com')  t1 = threading.Thread(target=r.resolve_domain) t1.start()  t2 = threading.Thread(target=r.generate_website_thumbnail) t2.start() 

But should I use them outside the Class? Should I write another Class to handle Threads?

What is the right way to do that?

like image 525
nacholibre Avatar asked Mar 12 '13 15:03

nacholibre


People also ask

How do I run a Python thread class?

run() method is an inbuilt method of the Thread class of the threading module in Python. This method is used to represent a thread's activity. It calls the method expressed as the target argument in the Thread object along with the positional and keyword arguments taken from the args and kwargs arguments, respectively.

How do you run a class in a thread?

Java Thread run() methodThe run() method of thread class is called if the thread was constructed using a separate Runnable object otherwise this method does nothing and returns. When the run() method calls, the code specified in the run() method is executed. You can call the run() method multiple times.

What is run method in Python?

The run method designates thread body. The run method gets it code on two ways. One is when the run method is overridden in a subclass. Another is when a callable object is passed as a target through the constructor of the Thread class. Either way, one can formulate the run() method of a python thread.


2 Answers

If you call them from the class, it is as simple as:

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() 
like image 54
A. Rodas Avatar answered Oct 16 '22 08:10

A. Rodas


You can inherit Thread class in DomainOperation, in this way code would be more clean and easily understandable. you have to override a run() method.

from threading import Thread  class DomainOperations(Thread):     def __init__(self, *args, **kwargs):        super().__init__(*args, **kwargs)        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) 

And you will start your threads in this way.

if __name__ == '__main__':    thread1 = DomainOperations()    thread1.start() 
like image 21
Hafiz Hashim Avatar answered Oct 16 '22 10:10

Hafiz Hashim