multithreading in Python: extremely high CPU consumption -


Below is a simple timer's code that prints a new number in the command line every 0.1 seconds, and -

Along with - waiting for a keypress (entered). As soon as the entry is pressed, the timer is turned into pause, the printing stops and waits for another entry keypress, which starts counting / printing again.

The problem I have is that in the 'pause' situation, the CPU consumption increases to about 100% (and less than 1% when running / showing).

Why is that so? What am i doing wrong

Here the code goes:

  import time, thread, sys time_passed = 0 l = [] trig = True def input (L): "" "A query Wait for "raw_input () l.append (none) def timer ():" "" prints a number of milliseconds "" Global Time-Time Time Sleep (.1) Time-Tide + = 1 System .stdout.write (format (time_pass) + "\ r") running sys.stdout.flush () def (): "Toggle play / pause state if press enter" "" global trigger, while l Trigger: Timer () if L: L = [] trig = Wrong return trig, l def off (): Pause the "toggle" / enter game status if the "global" trig, L does not start: if l: l = [] Trig = Correct return trig, LDIF main (): waits for a key to press, while the timer is running or stops "global", triangle is true: thread. Start_new_thread (Input, (L,)) Trigger: # timer is running () and: # timer is closed () Main ()   

Thanks

OK, if its "turned off" if you turn it on, then tell: < / P>

  def stop (): "" "Toggles the pause / play position if the entry is pressed"] Trig = True Return trig, l   

There is no break / wait in this loop, it runs at full speed without returning to the system, while within running you call time.Sleep . By entering time.leep (0.1) before if will work according to your wish.

Also a friendly personal note, you can reapply it a little bit. With the same explanation working on globals, two functions make it a bit difficult to understand =).

I can do something like this:

  import time, thread, system, threading time_passed = 0 l = [] def input (L): "" a keypress Wait for "raw_input () l.append (none) DEF timer ():" "" Number is printed in every millisecond "Global time-tide time-tide + = 1 sys.stdout.write (format (time_pass) ) + "\ r") sys.stdout.flush () Def running (state): Toggle play / pause state if pressed "" "global L while true: time.sleep (.1) if State: Timer () If L: L = [] state = state return state def (main) is not: "" "waits for a compactor, while running or stopping timer" "global l state = true Is true, whereas: thread: start_new_thread (input, (l,) state = running (state) main ()   

and I think how toggle better toggle I somehow function Do not like the idea of ​​returning from, you really do not have to go back Flip the Ajy but my guess is just personal preference.

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -