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

Time:08-27

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()

CodePudding user response:

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'])
  • Related