Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
48 views
in Technique[技术] by (71.8m points)

How to do multithreading or multiprocessing while calling function in python

I have sample dictionary is below

  • I need to find the count of each attribute
  • I need to implement using multithreading
  • total(todos) is the main function. In this there are 3 other functions called. which i need to implement using multithreading. Right now first it will do sequentially like first it will do userid_count(todos) followed by title_count(todos) and complete_count(todos). Since each of these function is independent of each other I need to do with multithreading/multiprocessing
    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):
        user_count = userid_count(todos)
        ###### Multithreading need to implement ##########
        title = title_count(todos)
        completed = complete_count(todos)
        search_count_all = {**user_count, **title, **completed}
        return search_count_all
    
    def userid_count(todos)
        #return userid count there are 2 user id 1, 2
        #{"userid":2}
        pass
    def title_count(todos)
        #return title count there are 3 title A, B, C
        #{"title":3}
        pass
    def complete_count(todos)
        #return completed count there are Tru and False
        #{"True": 1, "False":3}
        pass
    
    total(todos)

Expected out is [{"userid":2},{"title":3},{"True": 1, "False":3} ]

question from:https://stackoverflow.com/questions/65558270/how-to-do-multithreading-or-multiprocessing-while-calling-function-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can do this using multithreading.

import threading, queue
user_count_result = queue.Queue()
title_count_result = queue.Queue()
complete_count_result = queue.Queue()
def total(todos):
    user_countThread = threading.Thread(target=userid_count,args=(todo,user_count_result,))
    titleThread = threading.Thread(target=title_count,args=(todo,title_count_result,))
    completedThread = threading.Thread(target=complete_count,args=(todo,complete_count_result,))

    user_countThread.start()
    titleThread.start()
    completedThread.start()

    user_countThread.join()
    titleThread.join()
    completedThread.join()


    search_count_all = {**user_count_result.get(), **title_count_result.get(), **complete_count_result.get()}
    return search_count_all

def userid_count(todos, user_count_result)
    #Your logic here
    user_count_result.put({"userid": 2})

    
def title_count(todos, title_count_result)
    #Your logic here
    title_count_result.put({"userid": 2})
    
def complete_count(todos, complete_count_result)
    #Your logic here
    complete_count_result.put({"userid": 2})
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...