I am modifying a script that will be run as a windows scheduled task after hours. This script was written by another programmer previously.
The original script includes threading in the code. Should a script that is run as a single job from a scheduler require threading?

7 Years
Discussion Span
Last Post by wildgoose

I don't know Python but from an architectural point of view your answer is YES!

You said after hours, which implies that nobody is using the computer so it is free to use 100% of its time doing the processing.

Dual Core machine doing single-threadded means less then 50% of the computer is in use.
Quad Core machine doing single-threaded means less then 25% the machine is in use.

So with multi-threaded and a locking mechanism when the common source and destination data resource is accessed, that means that 100% of the machine is in use to do the job! Typically 2 threads per processor. So a quad core would use eight threads to be most efficient!

In a way its a good thing you asked because otherwise you're breaking cardinal sin number one when inheriting code. "Changing it without FULLY understanding it!"


So you are speaking about the performance gain from writing multithreaded code, right? If I am understanding you right, a multithreaded script would lead to efficient usage of system processor resources.



If the multi-threaded script controls multiple threads simultaneously then the answer is yes.

For example if I want to write a program designed to crunch data and turn it from one form into another, but I still want to be able to use the computer to run other applications then I'll only task up to 1/2 or 3/4 the machine with the task to do using worker threads. Or I'll just task one thread with the crunch application. But if I want to use every computing cycle I can on the task then I'll launch an appropriate number of worker threads. Available CPU after that is typically around 0%.

Even if you think this kind of task will only be run light (meaning one thread) you should write it multi-threaded so that you can flip a soft switch to crank it up, or slow it down, just in case you need to run a GUI or something. It won't be very responsive acknowledging the slowdown however! You'll find that you will still need to insert micro-naps into each workers thread in your multi-threaded code so that they'll work well among themselves!

Votes + Comments
Great knowledge of architecture and design

Thanks for the explanation. From a performance efficiency stand I now understand the need for multi-threading. But my original post was about multithreading as related to data integrity.

If this script did use only one thread to perform its functions, it would still run fine, except that it would under utilize server resources, right?
BTW, the script collects data off the internet and writes it to the database and performs calculations on this data and writes the results to the database.


using locks between threads when data contention is encountered.
Lock: Mutex, Semaphore, Critical-Section are three types of locks.

For example Let's say you have a list of numbers to determine if they are prime. All threads that need access to the list set the lock and wait. The Operating System will release one thread, it gets its number then releases the lock, and then chews on the number. When it is done with the number it locks and waits again. Other threads will be awakend and do the same operation.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.