Giacomo Ciampoli
Giacomo Ciampoli

Reputation: 821

python threads: first approaching

i'm new to python threads, as first task i wrote this:

from twitterHandler import Twitter_User
from text_analyzer import text_analyzer
import threading

if __name__=='__main__':
    usersIDS = {'user1':24503301,'user2':7375922343546478338,'user3':2144265434,'user4':50090727}
    threads = {}

    def get_data(user_id):
    '''get the most common words used by twitter user un his text tweet and save it in a file object'''
        ...

    for user_name, user_id in usersIDS.items():
        t = threading.Thread(target=get_data,args=(user_id,))
        threads[user_name] = t
        print('Starting to get data for: {}'.format(user_name)
        t.start()


    for name,t in threads.items():
        t.join()
        print('Process for {} Stopped'.format(name))

The code works, but i'm wondering if that is a typical use case for threads, or i simply could do something like that.

for user_id in usersIDS.values():
    get_data(user_id)
    ...

In other words, is multithreading the right choice for my problem? many thanks

Upvotes: 1

Views: 59

Answers (1)

BatyaGG
BatyaGG

Reputation: 734

Yes, in my opinion it is good approach and pretty common. Threads are used in cases such as:

  • Asynchronous operations - when some process does not depend on output of the other
  • Processes which can be parrallelized - like doing filtering on different parts of image
  • Operations running on backgroud

I think your case can be treated as first and second groups. Each person object analysis does not depend on the outputs of other people analysis. However, in case you have big data of users and words, it can cause problems for your computer, since it have to manage creation and destruction of each thread. Anyway, it is personal decision use threads or not.

Upvotes: 1

Related Questions