Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Threads within a Thread in Python

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.

like image 320
Kev1n91 Avatar asked Apr 14 '17 21:04

Kev1n91


People also ask

Can a thread create another thread Python?

Yes, spawning threads from within another thread is OK. Just remember that there exists a main Python thread, that governs the others.

Can we create thread inside thread?

Yes a thread can launch another thread, and that thread can launch thread(s) and on and on... In the run() method of a thread - you can create and start other threads.

How do you make two threads in Python?

Creating Thread Using Threading Module Define a new subclass of the Thread class. Override the __init__(self [,args]) method to add additional arguments. Then, override the run(self [,args]) method to implement what the thread should do when started.

Can Python have multiple threads?

To recap, threading in Python allows multiple threads to be created within a single process, but due to GIL, none of them will ever run at the exact same time. Threading is still a very good option when it comes to running multiple I/O bound tasks concurrently.


1 Answers

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]
like image 194
Tim Peters Avatar answered Sep 29 '22 12:09

Tim Peters