Python threading interrupt sleep

The correct approach is to use threading.Event. For example: import threading e = threading.Event() e.wait(timeout=100) # instead of time.sleep(100) In the other thread, you need to have access to e. You can interrupt the sleep by issuing: e.set() This will immediately interrupt the sleep. You can check the return value of e.wait to determine whether … Read more

std::async won’t spawn a new thread when return value is not stored

From just::thread documentation: If policy is std::launch::async then runs INVOKE(fff,xyz…) on its own thread. The returned std::future will become ready when this thread is complete, and will hold either the return value or exception thrown by the function invocation. The destructor of the last future object associated with the asynchronous state of the returned std::future … Read more

Memory usage in C#

If you want the memory of the entire running process and not on a per thread basis, how about: // get the current process Process currentProcess = System.Diagnostics.Process.GetCurrentProcess(); // get the physical mem usage long totalBytesOfMemoryUsed = currentProcess.WorkingSet64; There’s a whole host of other process memory properties besides WorkingSet64 check out the “memory related” ones … Read more

Embedding python in multithreaded C application

I had exactly the same problem and it is now solved by using PyEval_SaveThread() immediately after PyEval_InitThreads(), as you suggest above. However, my actual problem was that I used PyEval_InitThreads() after PyInitialise() which then caused PyGILState_Ensure() to block when called from different, subsequent native threads. In summary, this is what I do now: There is … Read more

Wait for QueueUserWorkItem to Complete

You could use events to sync. Like this: private static ManualResetEvent resetEvent = new ManualResetEvent(false); public static void Main() { ThreadPool.QueueUserWorkItem(arg => DoWork()); resetEvent.WaitOne(); } public static void DoWork() { Thread.Sleep(5000); resetEvent.Set(); } If you don’t want to embed event set into your method, you could do something like this: var resetEvent = new ManualResetEvent(false); … Read more