Define multi functions getting their names from a data frame and run all with thread in python

Question:

I want to define functions getting their name from a dataframe and run all at the same time.
I tried but functions are not starting:

import pandas as pd
import threading
import time
import inspect

df = pd.read_csv('../cities-list.csv')
for i in df["cities"]:
    define_func = f"""
    
    def task_{i}():
        for i in range(0,5):
            time.sleep(i)
            print(i)
    """
    define_func = inspect.cleandoc(define_func)
    exec(define_func)
    Thread = threading.Thread(target=exec(define_func))
    Thread.start()
    Thread.join()
Asked By: fyec

||

Answers:

Looks like you want to process each city in a separate thread in which case:

import pandas as pd
from concurrent.futures import ThreadPoolExecutor

df = pd.read_csv('../cities-list.csv')

def process(city):
    print(f'Processing {city}')

with ThreadPoolExecutor() as executor:
    executor.map(process, df['cities'])
Answered By: Vlad
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.