Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python ThreadPoolExecutor on method of instance

In a Python project, I use ThreadPoolExecutor to multithreading my program. I use one thread by instance of object in order to run one method. At this time, I use this syntaxe :

class Fqdn():
    def port_scan(self):
        # Do a TCP scan
        pass

# Used because I must submit a function with ThreadPoolExecutor
def port_scan(fqdn):
    fqdn.port_scan()

with concurrent.futures.ThreadPoolExecutor(max_workers=THREAD_LIMIT) as executor:
    # fqdn_list is a list of Fqdn instances
    for fqdn in fqdn_list:
        executor.submit(port_scan, fqdn)

Do you have any idea which lets me to submit an method instance to my executor ?

like image 402
Samuel Dauzon Avatar asked Jul 01 '15 10:07

Samuel Dauzon


1 Answers

Pass the bound method, fqdn.port_scan:

with concurrent.futures.ThreadPoolExecutor(max_workers=THREAD_LIMIT) as executor:
    # fqdn_list is a list of Fqdn instances
    for fqdn in fqdn_list:
        executor.submit(fqdn.port_scan)
like image 183
unutbu Avatar answered Sep 28 '22 08:09

unutbu