![]() |
attempted multithread doesn't multithread - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: attempted multithread doesn't multithread (/thread-6848.html) |
attempted multithread doesn't multithread - meems - Dec-10-2017 import threading,time def simple_task(s): for j in range(5): print(s) time.sleep(0.5) n=5 thread_list=[] for i in range(n): t=threading.Thread(target=simple_task(i)) t.start() thread_list.append(t)expected result 0,1,2,3,4,0,1,2....etc actual 0,0,0,0,0,1,1,1 etc I've tried adding t.daemon=True, that doesn't change the result. I seem to be missing something. But haven't a clue what. Anyone know? thx RE: attempted multithread doesn't multithread - snippsat - Dec-10-2017 Call function like this when using threading. import threading,time def simple_task(s): for i in range(5): print(s) time.sleep(0.5) n = 5 thread_list = [] for i in range(n): t = threading.Thread(target=simple_task, args=(i,)) thread_list.append(t) t.start() RE: attempted multithread doesn't multithread - meems - Dec-10-2017 thx. That works. After a a bit of head scratching seems I've got a wrong idea on how python handles function args. def times(x,y): return y*x def apply_func(f,x): return f(x) print (apply_func(times,(2,3)))expected outcome : 6 actual outcome : error, don't like tuple as args for function times Function parameter ( arg ) lists are written with the syntax of tuples, but they are not tuples. Attempt to pass a tuple as an arg list results in an error. So if the tuple (2,3) is not appropriate here... what is? my trick to bypass python's arg list ettiquette : always pack arg lists into a tuple. then have your functions take one parameter. 1st line of function is to unpack the tuple e.g. def my_funct(my_args): x,y,z = my_args # function code hereahahaha, i beat the system def times(z): (x,y)=z return y*x def apply_func(f,x): return f(x) print (apply_func(times,(2,3)))works here RE: attempted multithread doesn't multithread - snippsat - Dec-10-2017 Use Ctrl+shift+v when paste in code to preserve indentation.(Dec-10-2017, 02:56 PM)meems Wrote: ahahaha, i beat the systemGood hack,but there is a normal way to solve this in Python. Look into *args and **kwargs .def times(x, y): return y * x def apply_func(f, *args): return f(*args) print(apply_func(times,2,3))
def test(a, b): print(a + b) def apply_func(func, **kwargs): for i in range(5): func(*tuple(value for _, value in kwargs.items())) if __name__ == '__main__': apply_func(test, a=2, b=8)
RE: attempted multithread doesn't multithread - meems - Dec-10-2017 i think mine's better. because it requires less concepts, keeps the language simpler. I don't understand why the python writers invoked *args and **kwargs when they already had objects like lists. In the context of this thread, can tuples, lists, *args or **kwargs be used in threading.Thread to get it to multithread properly? Or does python require a 5th 'multi element' type ( the iterator, implicitly defined when by the syntax (x,) heck, why not create a unique 'multi element' type for each function needing a 'multiple element' input. That's the pythonic way it seems. ![]() |