Reputation: 31
I am trying to run a code where I am working with Queue and threads. Below is the code snippet:
import threading
from Queue import Queue
def function():
results = []
for i in range(68):
return_list = function2(i)
results.append(return_list)
if return_list:
print("True returned")
else:
print("Returned false")
return results
def function2(i):
print("In function 2 with: " + str(i))
results = []
working_queue = Queue()
for _ in range(25):
worker = threading.Thread(target=function3, args=(working_queue, results))
worker.setDaemon(True)
worker.start()
for p in range(150):
working_queue.put(p)
working_queue.join()
return results
def function3(working_queue, results):
while True:
try:
current_item = working_queue.get()
print("Processing:" + str(current_item))
results.append("True")
except Exception as e:
print("An exception in function 3: " + str(e))
finally:
working_queue.task_done()
if __name__ == "__main__":
results = function()
print(str(results))
The code raises the following exception:
Traceback (most recent call last):
File "C:/pythonErrors/stackoverflow.py", line 45, in <module>
function()
File "C:/pythonErrors/stackoverflow.py", line 8, in function
return_list = function2(i)
File "C:/pythonErrors/stackoverflow.py", line 24, in function2
worker.start()
File "C:\Python27\lib\threading.py", line 736, in start
_start_new_thread(self.__bootstrap, ())
thread.error: can't start new thread
How can we delete the previously created and completed threads. So that with each for loop execution in function2()
new threads are not created.
The requirement is to create only 25 threads for the whole process (currently total 68x25 threads are being created)
Upvotes: 2
Views: 3483
Reputation: 31
Since, the requirement was to work with 25 threads in total; creation of threads should be the part of outermost function (i.e. function() here). Below is the solution:
import threading
from Queue import Queue
def function():
results = []
working_queue = Queue()
for _ in range(25):
worker = threading.Thread(target=function3, args=(working_queue, results))
worker.setDaemon(True)
worker.start()
for i in range(68):
return_list = function2(i, working_queue)
working_queue.join()
results.append(return_list)
if return_list:
print("True returned")
else:
print("Returned false")
return results
def function2(i, working_queue):
print("In function 2 with: " + str(i))
for p in range(150):
working_queue.put(p)
def function3(working_queue, results):
while True:
try:
current_item = working_queue.get()
print("Processing:" + str(current_item))
results.append("True")
except Exception as e:
print("An exception in function 3: " + str(e))
finally:
working_queue.task_done()
if __name__ == "__main__":
results = function()
print(str(results))
Upvotes: 1