12

I am using the threading libary and want to have one thread that will call several threads. The background to this program is that I have a camera which captures Image and makes them available in a class on a TCP-SocketServer.

Thus I need one thread that runs the camera capturing and a second thread that runs the TCPServer, but within this Thread there are several Threads for each incoming connection.

This last thread means I need a thread that can create threads on its own. Unfortunately this did not work.

I managed to break down the immense code into a small snippet which represents the problem:

import threading

def adder(x,res,i):
        res[i] = res[i] + x*i;

def creator(a,threads,results):
    results = []
    for i in range(0,a):
        results.append(0)
        threads.append(threading.Thread(target=adder,args=(a,results,i)))
        threads[i].start()
    for i in range(0,len(threads)):
        threads[i].join()

    return results;


threads = [];
results = [];

mainThread = threading.Thread(target=creator,args=([5,threads,results]))
mainThread.start()

mainThread.join()
for i in range(0,len(results)):
    print results[i]
    print threads[i]

In the function creator which is called as a thread there should be several threads created with the funciton adder.

However the results are empty, why is that so?

This is the same problem that occurs in my larger program.

7
  • 1
    Python threads have no such method (.terminate()) - regardless of whether they've been started. See here: stackoverflow.com/questions/323972/… Commented Apr 14, 2017 at 21:42
  • 1
    Python threads have no .stop() method either - your edited code should be raising an exception about that now. See the link I already gave: Python supports no builtin way to force threads to stop. Commented Apr 14, 2017 at 22:06
  • 1
    Strangely it worked. However I now replaced the thread to one that will just have a simple task and wait for all the threads to join , however the results are still empty and I get now the notification that NoneType has no function called join Commented Apr 14, 2017 at 22:22
  • You're appending the start return values to threads. Commented Apr 14, 2017 at 22:22
  • Thank you! That resolved this issue, but still nothing in the results :/ Commented Apr 14, 2017 at 22:27

1 Answer 1

18

You got close! :-)

The problem in the latest version of the code is that, while the global results is passed to creator(), creator() never uses it: it creates its own local results list. Of course modifying the latter has no effect on the global results, so that one remains empty. So here's a variation to repair that, but also with minor local changes to make the code more "Pythonic":

import threading

def adder(x, res, i):
    res[i] += x*i

def creator(a, threads, results):
    for i in range(a):
        results.append(0)
        t = threading.Thread(target=adder, args=(a, results, i))
        threads.append(t)
        t.start()
    for t in threads:
        t.join()

threads = []
results = []

mainThread = threading.Thread(target=creator, args=(5, threads, results))
mainThread.start()
mainThread.join()
for i in range(len(results)):
    print results[i]
    print threads[i]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.