How to pass a list to Python threading.Thread
?
In the beflow example, I am converting the list to a dictionary. Is there a way to pass list as such to doWork
method?
def launcher():
#....
list = ['name1', 'name2', 'name3','name4']
nameList = { 'names' : list }
workThread = threading.Thread(target=self.doWork, kwargs=nameList)
workThread.start()
def doWork(self, **kwargs):
for i, name in enumerate(kwargs['nameList']):
print i, name
Threading in Python is simple. It allows you to manage concurrent threads doing work at the same time. The library is called “threading“, you create “Thread” objects, and they run target functions for you. You can start potentially hundreds of threads that will operate in parallel.
In fact, a Python process cannot run threads in parallel but it can run them concurrently through context switching during I/O bound operations. This limitation is actually enforced by GIL. The Python Global Interpreter Lock (GIL) prevents threads within the same process to be executed at the same time.
Use the Python threading module to create a multi-threaded application. Use the Thread(function, args) to create a new thread. Call the start() method of the Thread class to start the thread. Call the join() method of the Thread class to wait for the thread to complete in the main thread.
format(bar) return 'foo' + baz from multiprocessing. pool import ThreadPool pool = ThreadPool(processes=1) async_result = pool. apply_async(foo, ('world', 'foo')) # tuple of args for foo # do some other stuff in the main process return_val = async_result. get() # get the return value from your function.
The reason being that workThread = threading.Thread(target=doWork, args=lst)
takes the second argument as a list and then maps the contents of the lst
to the various arguments required by the method or function, Now since you want to pass only a single argument to the function which is a list, So if you see the common Syntax it is written some thing like: args = (param1, param2,...)
Now since you have only 1 argument so this reduces to args = (param1)
Now replacing the param1 with a list you will get args = ([1, 2, 3...])
, So actually the point here is that you need a nested list to pass as args
because the arguments are enclosed inside the list or a tuple so you need to wrap your list
inside a list or tuple. Any content under that wrapper list would be mapped with the arguments of the function or method being called.
To make the scenario clear:
If you pass args = [1, 2, 3, 4] then you mean that you are passing these 4 values as the arguments to that function or method.
On the other side if you pass
args = [[1, 2, 3, 4]]
orargs = ([1, 2, 3, 4], )
orargs = ((1, 2, 3, 4), )
Then only the whole list or tuple would be passed as a parameter to the function or method called.
I have simplified your code snippet to look like :
import threading
lst = ['name1', 'name2', 'name3','name4', "a"]
def doWork(param):
for i, name in enumerate(param):
print i, name
workThread = threading.Thread(target=doWork, args=(lst,))
workThread.start()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With