python sleep in thread

Questions about Wine on Linux
Locked
Ebiko
Level 1
Level 1
Posts: 9
Joined: Fri Dec 03, 2021 8:30 am

python sleep in thread

Post by Ebiko »

Hello there.

I've noticed something weird, and wanted to ask what the cause for this could be.
(It is highly likely that its an wine issue. Because the same application running in Windows will not create this weird issue. )

Whats the issue then:

I have an application that has a built in Cython/Python 2.7 thats the background.

this Application is running fine within wine.
No issues.

I am trying to rework some functions and methods to use Sleep, to free up some Processing power.
But i dont want to call sleep in the main thread, because it will freeze the application.
Thus i am trying to use threads (module thread) because threading is not available for me in this built application.

But here is the issue.
time.sleep(1) works fine in the main thread. It will sleep approximately 1.0001 s or slightly above. but thats accurate enough.

as soon as i run the following code

Code: Select all

def sleep():
    print("Speed: " + str(0.125))
    sleepS = time.time()
    time.sleep(0.125)
    sleepE = time.time()
    print("Time Slept: " + str(sleepE-sleepS))
thread.start_new_thread(sleep,())
i am getting an weird result:

Code: Select all

Speed: 0.125
Time Slept: 0.1450
Speed: 0.125
Time Slept: 0.5362
Speed: 0.125
Time Slept: 1.2624
Speed: 0.125
Time Slept: 0.3734
Speed: 0.125
Time Slept: 0.7329
Speed: 0.125
Time Slept: 2.723
so as soon as it runs in a thread, its becoming almost random. - Thus beeing said, on windows this code runs also quite well (with max 0.05 seconds difference in between, which is good enough.)
One time i even had a 3! second sleep but actually used again an 0.125 second sleep.
Does wine run threads differently somehow ?
or are threaded applications not well supported currently?

thanks

Ebiko
warrenfelsh
Newbie
Newbie
Posts: 1
Joined: Tue May 17, 2022 1:27 am

Re: python sleep in thread

Post by warrenfelsh »

Python sleep() will pause for an hour, day or whatever if given the proper value. It does not allow other processes take place (in same script) however. A better way is to use an event which will create an event on timeout.

Have a look at threading.Timer. It runs your function in a new thread without using sleep().

from threading import Timer

Code: Select all

def hello():
    print "hello, world"

t = Timer(30.0, hello)
t.start() # after 30 seconds, "hello, world" will be printed
The second method to delay would be using the implicit wait method:

Code: Select all

driver.implicitly_wait(5)
The third method is more useful when you have to wait until a particular action is completed or until an element is found:

Code: Select all

self.wait.until(EC.presence_of_element_located((By.ID, 'UserName'))
Locked