Sunday, 10 January 2021

How to implement Multithreading/Multiprocessing in Python by merging the function

The dictionary and code is below which is working

  • The question is regarding mainly on multithreading, I know that below code can be easily rewrite to easy way. But the ask is on multithreading, I just created one working example to test
  • total(todos) is the main function
  • user_count , title_count , complete_count are independent of each other
  • I need to implement multithreading/multiprocessing has to implement
  • def total(todos): is the place where need to do multithreading
todos = [{'userId': 1, 'id': 1, 'title': 'A', 'completed': False},
     {'userId': 1, 'id': 2, 'title': 'B ', 'completed': False},
     {'userId': 1, 'id': 1, 'title': 'C', 'completed': False},
     {'userId': 1, 'id': 2, 'title': 'A', 'completed': True},
     {'userId': 2, 'id': 1,'title': 'B', 'completed': False}]
def total(todos):
    ###### Multithreading need to implement ##########
    user_count = userid(todos)
    title_count = title(todos)
    complete_count = completed(todos)
    search_count_all = {**user_count, **title_count, **complete_count}
    return search_count_all
def userid(todos):    
    for d in todos:
        for l, m in d.items():  
            super_dict.setdefault(l, []).append(m)
    d = {k:len(set(v)) for k,v in super_dict.items()}
    return {"userid":d['userId']}
def title(todos):    
    for d in todos:
        for l, m in d.items():  
            super_dict.setdefault(l, []).append(m)
    d = {k:len(set(v)) for k,v in super_dict.items()}
    return {"title":d['title']}
def completed(todos):    
    for d in todos:
        for l, m in d.items():  
            super_dict.setdefault(l, []).append(m)
    d = {k:len(set(v)) for k,v in super_dict.items()}
    return {"completed":d['completed']}

total(todos)

Current output and expected output

{'userid': 2, 'title': 4, 'completed': 2}

can we do multprocessing also

from joblib import Parallel, delayed



from How to implement Multithreading/Multiprocessing in Python by merging the function

No comments:

Post a Comment