Python: Understanding Threading Module

2024/11/19 22:29:26

While learning Python's threading module I've run a simple test. Interesting that the threads are running sequentially and not parallel. Is it possible to modify this test code so a program executes the threads in same fashion as multiprocessing does: in parallel?

import threadingdef mySlowFunc(arg):print "\nStarting...", argm=0for i in range(arg):m+=iprint '\n...Finishing', argmyList =[35000000, 45000000, 55000000]for each in myList:thread = threading.Thread(target=mySlowFunc, args=(each,) )thread.daemon = Truethread.start()thread.join()print "\n Happy End \n"

REVISED CODE:

This version of the code will initiate 6 'threads' running in 'parallel'. But even while there will be 6 threads only two CPU's threads are actually used (6 other Physical CPU threads will be idling and doing nothing).

import threadingdef mySlowFunc(arg):print "\nStarting " + str(arg) + "..."m=0for i in range(arg):m+=iprint '\n...Finishing ' + str(arg)myList =[35000000, 45000000, 55000000, 25000000, 75000000, 65000000]for each in myList:thread = threading.Thread(target=mySlowFunc, args=(each,) )thread.daemon = Falsethread.start()print "\n Bottom of script reached \n"
Answer

From the docs for the join method:

Wait until the thread terminates. This blocks the calling thread until the thread whose join() method is called terminates – either normally or through an unhandled exception – or until the optional timeout occurs.

Just create a list of threads and join them after launching every single one of them.

Edit:

The threads are executing in parallel, you can think of python's threads like a computer with a single core, the thing is, python's threads are best for IO operations (reading/writing a big file, sending data through a socket, that sort of thing). If you want CPU power you need to use the multiprocessing module

https://en.xdnf.cn/q/118488.html

Related Q&A

How the program has control with the break statement [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Python: how to plot data coming from different excel sheets in the same chart

I need to create an interactive chart on python taking data from different sheets of an Excel file. I tried to create a for loop to take data from all the sheets automatically, but I manage to graph on…

Dynamic html table with django [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 6…

Driving costs - functions in python- EOFerror in if __name__ == __main__:

I am stuck with this question: Write a function driving_cost() with input parameters miles_per_gallon, dollars_per_gallon, and miles_driven, that returns the dollar cost to drive those miles. All items…

Pandas loc dynamic conditional list

I have a Pandas DataFrame and I want to find all rows where the ith column values are 10 times greater than other columns. Here is an example of my DataFrame:For example, looking at column i=0, row B (…

no module named numpy python2.7

Im using python 2.7 on Linux CentOS 6.5. After successfully using yum to install numpy, I am unable to import the module.from numpy import *The above code produces the following error:no module named …

TypeError: list indices must be integers or slices, not tuple for list of tuples

I am getting "list indices must be integers or slices, not tuple" error while trying to generate list from list of tuples. list of tuples have the following structure:[(29208, 8, 8, 8), (2920…

Spark Unique pair in cartesian product

I have this:In [1]:a = sc.parallelize([a,b,c]) In [2]:a.cartesian(a).collect() Out[3]: [(a, a), (a, b), (a, c), (b, a), (c, a), (b, b), (b, c), (c, b), (c, c)]I want the following result:In [1]:a = sc.…

How to use double click bid manager(DBM) API in python

I am trying to use the google Double click bid manager (DBM) API, to download reports, I am trying to make this automatic without manual authentication, but all I can find is the GitHub repo for DBM sa…

How can I replace a value in an existing excel csv file using a python program?

How can I update a value in an existing .csv file using a python program. At the moment the file is read into the program but I need to be able to change this value using my program, and for the change…