0

I have the following code. What i am trying to do is to run many instances of function. In this case for example, the function bigbangtheory() would return df. Could you please advise how can i save the output of the function if i run this code in multithread.

import threading
import numpy as np
import pandas as pd 

def bigbangtheory():
    new = {'name': ['Sheldon', 'Penny', 'Amy', 'Bernadette', 'Raj', 'Howard '],
                    'episodes': [31, 24, 31, 29, 37, 40],
                    'gender': ['male', 'female', 'female', 'female', 'male', 'male']}
    
    a = pd.DataFrame(new, columns = ['name','episodes', 'gender'])
    
    return a
def bigbangtheory1():
    
    old = {'name': ['Sheldon', 'Penny', 'Amy', 'Bernadette', 'Raj', 'Howard'],
                    'episodes': [12, 32, 31, 32, 37, 40],
                    'gender': ['male', 'female', 'female', 'female', 'male', 'male']} 
    b = pd.DataFrame(new, columns = ['name','episodes', 'gender'])
    return b
     
if __name__ == "__main__":
    # creating thread
    t1 = threading.Thread(target=bigbangtheory)
    t2 = threading.Thread(target=bigbangtheory1)

    t1.start()
    t2.start()

    t1.join()
    t2.join()
Slartibartfast
  • 1,058
  • 4
  • 26
  • 60
  • 2
    Where do you want to save it? – Tarik Sep 19 '21 at 02:21
  • any variable, i am not sure how to do that – Slartibartfast Sep 19 '21 at 02:22
  • Another related question: https://stackoverflow.com/questions/1312331/using-a-global-dictionary-with-threads-in-python where you could probably create a global dictionary with [threading.get_ident()](https://docs.python.org/3/library/threading.html#threading.get_ident) as key and the resulting dataframe as value. Though I believe the usage of [Queue](https://docs.python.org/3/library/queue.html) is preferable as it is thread-safe. – Niel Godfrey Pablo Ponciano Sep 19 '21 at 03:08
  • Your code will run sequentially unless you do heavy I/Os to read your data frames. – Tarik Sep 19 '21 at 05:43
  • @Tarik It is becuase i have `.join()` would it run concurrently if i remove it? – Slartibartfast Sep 19 '21 at 21:15

0 Answers0