sleep

Python3 sleep() problem

北慕城南 提交于 2019-11-27 22:17:06
I was writing a simple program on Python 3.1 and I stumbled upon this: If I run this on the IDLE it works as intended - prints "Initializing." and then adds two dots, one after each second, and waits for input. from time import sleep def initialize(): print('Initializing.', end='') sleep(1) print(" .", end='') sleep(1) print(" .", end='') input() initialize() The problem is that when I double-click the .py to execute the file, it runs on python.exe instead of pythonw.exe, and strange things happen: it joins all the sleep() times i.e. makes me wait for 2 seconds, and then prints the whole

SwitchToThread vs Sleep(1)

有些话、适合烂在心里 提交于 2019-11-27 21:43:03
问题 I'm wondering what's the actual difference between calling Thread.Sleep(1) and calling SwitchToThread (if we ignore that it's currently not exposed by the BCL). Joe Duffy mentions in his post that: "The kernel32!SwitchToThread API doesn't exhibit the problems that Sleep(0) and Sleep(1) do." (regarding the scheduler's behavior) Why won't Sleep behave exactly like SwitchToThread? Why this differentiation exist, and for what is it good for? (if at all..) 回答1: There are two differences. The first

ManualResetEvent vs. Thread.Sleep

会有一股神秘感。 提交于 2019-11-27 21:28:28
I implemented the following background processing thread, where Jobs is a Queue<T> : static void WorkThread() { while (working) { var job; lock (Jobs) { if (Jobs.Count > 0) job = Jobs.Dequeue(); } if (job == null) { Thread.Sleep(1); } else { // [snip]: Process job. } } } This produced a noticable delay between when the jobs were being entered and when they were actually starting to be run (batches of jobs are entered at once, and each job is only [relatively] small.) The delay wasn't a huge deal, but I got to thinking about the problem, and made the following change: static ManualResetEvent

Does thread.yield() lose the lock on object if called inside a synchronized method?

∥☆過路亽.° 提交于 2019-11-27 21:25:29
问题 I understand that Thread.currentThread().yield() is a notification to thread scheduler that it may assign cpu cycle to some other thread of same priority if any such is present. My question is: If current thread has got lock on some object and calls yield() , will it loses that lock right away? And when thread scheduler finds out there is no such thread to assign cpu cycle, then the thread which has called yield() will again be in fight to get lock on the object which it has lost earlier?? I

Significance of Sleep(0)

帅比萌擦擦* 提交于 2019-11-27 21:00:07
I used to see Sleep(0) in some part of my code where some infinite/long while loops are available. I was informed that it would make the time-slice available for other waiting processes. Is this true? Is there any significance for Sleep(0) ? According to MSDN's documentation for Sleep : A value of zero causes the thread to relinquish the remainder of its time slice to any other thread that is ready to run. If there are no other threads ready to run, the function returns immediately, and the thread continues execution. The important thing to realize is that yes, this gives other threads a

How to asynchronously wait for x seconds and execute something then?

淺唱寂寞╮ 提交于 2019-11-27 20:37:58
I know there is Thread.Sleep and System.Windows.Forms.Timer and Monitor.Wait in C# and Windows Forms. I just can't seem to be able to figure out how to wait for X seconds and then do something else - without locking the thread. I have a form with a button. On button click a timer shall start and wait for 5 seconds. After these 5 seconds some other control on the form is colored green. When using Thread.Sleep , the whole application would become unresponsive for 5 seconds - so how do I just "do something after 5 seconds"? eFloh (transcribed from Ben as comment) just use System.Windows.Forms

Bash: infinite sleep (infinite blocking)

时光毁灭记忆、已成空白 提交于 2019-11-27 19:15:22
问题 I use startx to start X which will evaluate my .xinitrc . In my .xinitrc I start my window manager using /usr/bin/mywm . Now, if I kill my WM (in order to f.e. test some other WM), X will terminate too because the .xinitrc script reached EOF. So I added this at the end of my .xinitrc : while true; do sleep 10000; done This way X won't terminate if I kill my WM. Now my question: how can I do an infinite sleep instead of looping sleep? Is there a command which will kinda like freeze the script?

JavaScript sleep [duplicate]

北城以北 提交于 2019-11-27 18:28:25
问题 This question already has an answer here: What is the JavaScript version of sleep()? 75 answers Yes, I know - that question has thousands of answers. please, don't tell me about setTimeout method because - yes, everything is possible with that but not so easy as using sleep() method. For example: function fibonacci(n) { console.log("Computing Fibonacci for " + n + "..."); var result = 0; //wait 1 second before computing for lower n sleep(1000); result = (n <= 1) ? 1 : (fibonacci(n - 1) +

Detect OS Sleep and Wake Up events in Java

核能气质少年 提交于 2019-11-27 18:04:18
问题 Is there a way for a Java program to detect when the operating system is about to go to sleep, or failing that, at least detecting a wake up? The actual problem is that in a particular application a number of MySQL database operations are run in the background. In testing on a Windows machine these database transactions are interrupted after a sleep/wake-up cycle causing a ton of error conditions in the program. These errors typically look something like this: java.net.SocketException MESSAGE

Python time.sleep() vs event.wait()

孤人 提交于 2019-11-27 17:34:58
I want to perform an action at a regular interval in my multi-threaded Python application. I have seen two different ways of doing it exit = False def thread_func(): while not exit: action() time.sleep(DELAY) or exit_flag = threading.Event() def thread_func(): while not exit_flag.wait(timeout=DELAY): action() Is there an advantage to one way over the other? Does one use less resources, or play nicer with other threads and the GIL? Which one makes the remaining threads in my app more responsive? (Assume some external event sets exit or exit_flag , and I am willing to wait the full delay while