# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3 (ipykernel)
#     language: python
#     name: python3
# ---

# +
import multiprocessing

def square_list(mylist, result, square_sum):
    """
    function to square a given list
    """
    # append squares of mylist to result array
    for idx, num in enumerate(mylist):
        result[idx] = num * num

    # square_sum value
    square_sum.value = sum(result)

    # print result Array
    print("Result(in process p1): {}".format(result[:]))

    # print square_sum Value
    print("Sum of squares(in process p1): {}".format(square_sum.value))

if __name__ == "__main__":
    # input list
    mylist = [1,2,3,4]

    # creating Array of int data type with space for 4 integers
    result = multiprocessing.Array('i', 4)

    # creating Value of int data type
    square_sum = multiprocessing.Value('i')

    # creating new process
    p1 = multiprocessing.Process(target=square_list, args=(mylist, result, square_sum))

    # starting process
    p1.start()

    # wait until the process is finished
    p1.join()

    # print result array
    print("Result(in main program): {}".format(result[:]))

    # print square_sum Value
    print("Sum of squares(in main program): {}".format(square_sum.value))


# +
# importing the multiprocessing module
import multiprocessing

def print_cube(num):
    """
    function to print cube of given num
    """
    return("Cube: {}".format(num * num * num))

def print_square(num):
    """
    function to print square of given num
    """
    return("Square: {}".format(num * num))

if __name__ == "__main__":
    # creating processes
    p1 = multiprocessing.Process(target=print_square, args=(10, ))
    p2 = multiprocessing.Process(target=print_cube, args=(10, ))

    # starting process 1
    p1.start()
    # starting process 2
    p2.start()

    # wait until process 1 is finished
    p1.join()
    # wait until process 2 is finished
    p2.join()

    # both processes finished
    print("Done!")


# +
# importing the multiprocessing module
import multiprocessing
import pandas as pd

def add_column(df):
    """
    function to print cube of given num
    """
    df['test'] = 0


if __name__ == "__main__":
    
    df = pd.read_csv("../data/raw/computerscience_hot_posts.csv")
    
    # creating processes
    p1 = multiprocessing.Process(target=add_column, args=(df, ))

    # starting process 1
    p1.start()

    # wait until process 1 is finished
    p1.join()

    # both processes finished
    print("Done!")
    print(df)

# +
import time
from multiprocessing import Pool

def f(df, x):
    df['test'] = 0
    return df

if __name__ == '__main__':
    df = pd.read_csv("../data/raw/computerscience_hot_posts.csv")
    
    list_of_df = []
    
    for i, post_id in enumerate(df['post_id'].unique()):
        list_of_df.append((df[df['post_id'] == post_id], i))
    
    start_time = time.time()
    with Pool(1) as p:
        results = p.starmap(f, list_of_df)
    print("--- %s seconds ---" % (time.time() - start_time))
    
#     start_time = time.time()
#     results = []
#     for dff in list_of_df:
#         results.append(f(dff))
#     print("--- %s seconds ---" % (time.time() - start_time))
# -

df = pd.read_csv("../data/raw/computerscience_hot_posts.csv")
df

results[0]

pd.concat(results)



StarcoderdataJupyterScriptsDedupFiltered