Click here to Skip to main content
15,037,178 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
Thread is slower, timeout is set to 13 seconds

What I have tried:

for i in range(400):
	try:
		thread=consumer(quee)
		thread.setDaemon(True)
		thread.start()
               thread.excepthook()
	except:
		print "Working only with %s threads"%i
		break
Posted
Updated 20-Jul-21 10:53am
v2
Comments
Richard MacCutchan 21-Jul-21 3:38am
   
Like I said yesterday, creating 400 threads will just slow everything down.
Dasisqo 21-Jul-21 7:34am
   
If i run the code with the tread

for i in range(400):
try:
thread=consumer(quee)
thread.setDaemon(True)
thread.start()
except:
print "Working only with %s threads"%i
break

The thread will run fine for a second and will die without the functions not complete in the code.
I decided to set the thread with thread.excepthook()

for i in range(400):
try:
thread=consumer(quee)
thread.setDaemon(True)
thread.start()
thread.excepthook()
except:
print "Working only with %s threads"%i
break

The code will work fine with (0) threads but it is slower.
Richard MacCutchan 21-Jul-21 7:46am
   
There is really not enough information to go on here. What does consumer(quee) do? When you get an exception you need to print the actual details rather than your vague message. And it is far from clear what you are actually trying to achieve with threads that you cannot do without them.
Dasisqo 21-Jul-21 8:24am
   
I have understood from explanations here than python does work with one thread at a time, what i am trying is to make the thread to be multiple and execute each line of thread simultaneously. Printing out more threads of the function and execute each line at a time.

From my code edition which is
for i in range(400):
try:
thread=consumer(quee)
thread.setDaemon(True)
thread.start()
thread.excepthook()
except:
print "Working only with %s threads"%i
break

The function prints out one line and execute it. I am asking if there are other means to make the work multi threading and execute each line at a time.
Richard MacCutchan 21-Jul-21 9:26am
   
python does work with one thread at a time
I am not sure exactly what you mean by that. Threading is an inherent part of all computers and operating systems, without it nothing would work. But you need to understand its limitations. At the physical computer level a single CPU can only execute a single instruction at a time, so a series of instructions will run in sequence. So in order to run more than one thread the system has to interrupt the processor (or wait for an external interrupt) at which point it can start or restart another thread. By doing this at the micro or nano second level it can give the appearance of many programs running simultaneously. Most modern PC processor chips have multiple cores, so the system can run a thread in each core. So that may allow 2, 4, 8 (or in big systems many) threads to run truly parallel. However, thread switching creates an overhead in the operating system. Every time a processor is required to switch threads, then the active thread's environment must be saved and an inactive one selected from a queue. And the more threads you try to run the more time is spent in switching. So running 400 threads can lead to the system spending all its time switching threads and almost none letting the threads run. So constrain the number of threads to at most twice the number of available cores, and preferably less.

In your code above, the threads do run simultaneously, whatever the output that you see.

And finally, you have still not explained what actual problem you are trying to solve, and why you think that threads will help.

Creating 400 threads does nothing but slow your workload down. The system can only execute the same number of threads as you have cores in your CPU, usually 8 or less unless you have a higher-end CPU. So, creating 400 threads is pointless.

There is no way to speed up the threads other than rewriting and optimizing the code the thread is running, and this is assuming the bottleneck in your code is CPU-bound.
   
v2
Python is limited by the Global Interpreter Lock (GIL), which only allows one thread to execute at a given instance. This means that threading does not reduce program run times, regardless of how many cores your system has. There's a Stack Overflow discussion here: multithreading - python multi-threading slower than serial? - Stack Overflow[^] Note that this is quite old, and Python3 may change things. Google for slow python threads. There are plenty of resources that might help you choose another direction to improve your programs.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900