11

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.

Kev1n91
  • 3,553
  • 8
  • 46
  • 96
  • Python threads have no such method (`.terminate()`) - regardless of whether they've been started. See here: http://stackoverflow.com/questions/323972/is-there-any-way-to-kill-a-thread-in-python – Tim Peters Apr 14 '17 at 21:42
  • 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. – Tim Peters Apr 14 '17 at 22:06
  • 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 – Kev1n91 Apr 14 '17 at 22:22
  • You're appending the `start` return values to `threads`. – user2357112 Apr 14 '17 at 22:22
  • Thank you! That resolved this issue, but still nothing in the results :/ – Kev1n91 Apr 14 '17 at 22:27
  • You're not waiting for `mainThread` to end, so the actual main thread charges full speed ahead, and you're probably printing `results` before any other threads have had a chance to get started. – Tim Peters Apr 14 '17 at 23:28
  • D'oh, thank you! after putting mainthread.join() the result vector is still empty.... – Kev1n91 Apr 15 '17 at 10:35

1 Answers1

16

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]
Tim Peters
  • 67,464
  • 13
  • 126
  • 132