multithreading - Global variable vs Parameters in a Python thread -


i trying update dictionary , read list using multiple threads 1 better, better pass list , dictionary parameters in thread or use them in thread using global keyword.

ie.

mythread(list, list_semaphore, dict, dict_semaphore) 

vs

mythread():     global dict     global dict_semaphore     global list     global list_semaphore 

is not matter of threading, using parameters function work combination of parameters data, not specific global variables.

lets have this:

def foo(lst, lst_semaphore, dct, dct_semaphore):     do_some_nice_stuff() 

you can spam thread doind list or dict:

threading.thread(target=foo, args = (lst1, lst_semaphore1, dct1, dct_semaphore1)) threading.thread(target=foo, args = (lst2, lst_semaphore2, dct1, dct_semaphore1)) threading.thread(target=foo, args = (lst2, lst_semaphore2, dct2, dct_semaphore2)) 

if use globals have redefine function each combination:

def foo():     global lst1     global lst_semaphore1     global dct1     global dct_semaphore1     do_nice_stuff() ... def foo2():     global lst2     global lst_semaphore2     global dct2     global dct_semaphore2     do_nice_stuff()   threading.thread(target=foo) threading.thread(target=foo1) threading.thread(target=foo2) 

so, using parameters make code more reusable always.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -