Threading in Python, Please check the script

Dave Angel davea at davea.name
Wed Jan 14 07:37:59 EST 2015


On 01/14/2015 07:11 AM, Robert Clove wrote:
> Can u provide me the pseudo script.
>
>
You say you're a beginner.  If so, you shouldn't be trying to use 
threads, which are tricky.  I've been programming for 46 years, and I 
seldom have had to write multi-threading code.

But this looks like a school assignment.  And it looks like the code has 
little to do with the assignment.  So let's break down the assignment a bit.

Your assignment says the two threads will be called thread1 and thread2. 
  So why do you call them t1 and t2?

Your assignment says the two functions will be called func1 and func2. 
So why do you call them print_time and print_time1 ?

The assignment doesn't say anything and about running an external 
process.  Since that's also complex and error prone, perhaps you should 
keep it simple till everything else works.

Some general principles:

1) try not to do the same thing in several places.  You create threads 
in 3 places, and two of them are wrong or at least non-optimal.  Keep 
just the one where you do it right.

2) Use useful names for the various variables.  Except of course where 
the assignment specifies the name to be used.  Then use that name.

3) Learn one new concept at a time.  If you're concentrating on threads 
here, don't try to learn subprocess at the same time, unless that's what 
the assignment calls for.

4) Avoid chdir, especially in multithreaded programs.  Use an absolute 
path if you must when dealing with files.  Except for trivial programs, 
changing the working directory is likely to bite you in the foot 
somewhere else in the program.


-- 
DaveA



More information about the Python-list mailing list