toshbar toshbar - 5 months ago 10
Python Question

Python: Is there some method/function to pause python without stopping the current execution?

I'm trying to record streaming data from an external source in Python3.5. But when I do the following:

enable_streaming()
time.sleep(10)
stop_streaming()


It appears that the streaming functionality becomes enabled, python stops for 10 seconds, then streaming is stopped. Possibly, due to python stopping for 10 seconds due to functionality of time.sleep, no streaming data is actually recorded. I used wireshark to track the flow to data and it seems that the destination acknowledges that enable_streaming() is received.

Enable_streaming() is a function called from a dll.
The dll works in matlab so that leads me to believe that python's time.sleep() is the issue here.
I don't have access to the source code of the dll but due to it working in Matlab and it's just a simple return ctypes function, I have to assume the dll is correct and not the issue here.

The python code calling enable streaming is:

import ctypes as ct
shim = ct.cdll.LoadLibrary('PCdll')

def enable_streaming()
f = shim.enable_streaming
f.restype = ct.c_int #return is int
result = f()


So my question is: is there some sort of command in python that pauses but doesn't "suspend execution of the current thread" like time.sleep() does?

Attempted with no success. Seems like this isn't the issue:

import time
start = time.time()

enable_streaming()
while (time.time() - start < 10): # 10 seconds
pass # loop for 10 seconds

stop_streaming()

Answer

I'm not sure if this will fix it, as it doesn't seem like the streaming can do anything without a thread, but this is what I usually do when I need to do something for a certain period of time without blocking.

import time

start = time.time()

while (time.time() - start < 10): # 10 seconds
    # loop for 10 seconds
Comments