forum.vmspython.org Forum Index forum.vmspython.org
Forum system
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

time.sleep(1.0) delay 1.2 seconds

 
Post new topic   Reply to topic    forum.vmspython.org Forum Index -> Python for OpenVMS
View previous topic :: View next topic  
Author Message
voalp



Joined: 06 May 2019
Posts: 6

PostPosted: Mon May 06, 2019 12:41 pm    Post subject: time.sleep(1.0) delay 1.2 seconds Reply with quote

Using sleep in conjunction with threading the delay of 1s results in a delay 1.2 s.
I am using VMS 8.4-2L1 and python 2.7.12

the following python code

import time
import threading
import datetime

def TimeStr(text):
print datetime.datetime.now().strftime("%H:%M:%S.%f")[0:12]+" " + text

def t1():
while True:
TimeStr("t1 sleep 1s")
time.sleep(1.0)
TimeStr("t1")

Thread1 = threading.Thread(target = t1)
Thread1.daemon = True
Thread1.start()
for n in range(5):
TimeStr("main sleep 1s")
time.sleep(1.0)
TimeStr("main")

leads to the output

12:06:14.714 t1 sleep 1s
12:06:14.914 main sleep 1s
12:06:15.814 t1
12:06:15.814 t1 sleep 1s
12:06:16.014 main
12:06:16.014 main sleep 1s
12:06:17.014 t1
12:06:17.014 t1 sleep 1s
12:06:17.214 main
12:06:17.214 main sleep 1s
12:06:18.214 t1
12:06:18.214 t1 sleep 1s
12:06:18.414 main
12:06:18.414 main sleep 1s
12:06:19.414 t1
12:06:19.414 t1 sleep 1s
12:06:19.614 main
12:06:19.614 main sleep 1s
12:06:20.614 t1
12:06:20.614 t1 sleep 1s
12:06:20.814 main

e.g. I expected the time difference
in Thread1 between the lines
12:06:17.014 t1 sleep 1s
12:06:18.214 t1

to be exact 1.0 s, in fact the result is 1.2s.
When I try this example on fedora the result is 1.0s.
I tried this example with more parallel threads and the result gets more strange and complicated.

Is this a bug ?
How can I avoid this problem ?
Any hints ?

Thanks
Fritz
Back to top
View user's profile Send private message
jfp



Joined: 12 Jul 2004
Posts: 636

PostPosted: Wed May 15, 2019 12:23 pm    Post subject: Reply with quote

Hello,

Interesting, I reproduce the problem. I will take a look.

Using various duration, from 1 to 10s the delta is always 0.2s...

JF
Back to top
View user's profile Send private message Send e-mail Visit poster's website
jfp



Joined: 12 Jul 2004
Posts: 636

PostPosted: Wed May 15, 2019 12:36 pm    Post subject: Reply with quote

Using
Code:

import time
import threading
import datetime

ts = 1.0

def TimeStr(text):
    print datetime.datetime.now().strftime("%H:%M:%S.%f")[0:12]+" " + text

TimeStr("test")
time.sleep(0.1)
TimeStr("test")


def t1():
    while True:
        TimeStr("t1 sleep 1s")
        time.sleep(ts)
        TimeStr("t1")

Thread1 = threading.Thread(target = t1)
Thread1.daemon = True
Thread1.start()
for n in range(2):
    TimeStr("main sleep 1s")
    time.sleep(10 * ts)
TimeStr("main")

returns
Code:

17:00:39.046 test
17:00:39.155 test
17:00:39.156 t1 sleep 1s
17:00:39.356 main sleep 1s
17:00:40.256 t1
17:00:40.256 t1 sleep 1s
17:00:41.456 t1
17:00:41.456 t1 sleep 1s
17:00:42.656 t1
17:00:42.656 t1 sleep 1s
17:00:43.856 t1
17:00:43.856 t1 sleep 1s
17:00:45.056 t1
17:00:45.056 t1 sleep 1s
17:00:46.256 t1
17:00:46.256 t1 sleep 1s
17:00:47.456 t1
17:00:47.456 t1 sleep 1s
17:00:48.656 t1
17:00:48.656 t1 sleep 1s
17:00:49.356 main sleep 1s
17:00:49.856 t1
17:00:49.856 t1 sleep 1s
17:00:51.056 t1
17:00:51.056 t1 sleep 1s
17:00:52.256 t1
17:00:52.256 t1 sleep 1s
17:00:53.456 t1
17:00:53.456 t1 sleep 1s
17:00:54.656 t1
17:00:54.656 t1 sleep 1s
17:00:55.856 t1
17:00:55.856 t1 sleep 1s
17:00:57.056 t1
17:00:57.056 t1 sleep 1s
17:00:58.256 t1
17:00:58.256 t1 sleep 1s
17:00:59.356 main


As yo can see the main sleep is correct in this case.

JF
Back to top
View user's profile Send private message Send e-mail Visit poster's website
voalp



Joined: 06 May 2019
Posts: 6

PostPosted: Wed May 15, 2019 1:36 pm    Post subject: Reply with quote

Hallo jfp

Thank you for reply
I am a new to python.
I am very impressed about python and I am also very impressed about the potential using it on VMS.
I thank you very much bringing python to VMS --- very impressive

I want to use python to process messages in a time range of 0.1s in a threaded environment.
Is there a chance to correct the behavior of time.sleep in conjunction with threads ?

Thanks
Fritz

PS: Are there any plans to port python to VSI VMS X64 ?
Back to top
View user's profile Send private message
jfp



Joined: 12 Jul 2004
Posts: 636

PostPosted: Thu May 16, 2019 1:52 pm    Post subject: Reply with quote

Hello Fritz,

Thanks for your support.

I have take a look and do some test replacing the sleep by a call to sys$setimr with the same result...
So the problem seem to not be in the sleep routine but in the thread part.

I suspect that each thread switch introduce this .2s delay.

But if you use 10s instead of 1s the timer is correct...

I'm investigated.

JF
Back to top
View user's profile Send private message Send e-mail Visit poster's website
jfp



Joined: 12 Jul 2004
Posts: 636

PostPosted: Thu May 16, 2019 2:32 pm    Post subject: Reply with quote

Found a workaround:
Code:
set image PYTHON_ROOT:[VMS.BIN]PYTHON.exe/flag=UPCALLS


Seem to fix the problem.

JF
Back to top
View user's profile Send private message Send e-mail Visit poster's website
voalp



Joined: 06 May 2019
Posts: 6

PostPosted: Thu May 16, 2019 5:30 pm    Post subject: Reply with quote

Hello JF

Works fine.
Now even starting threads every 0.05s is no problem.

Thank you very much !!!!!
javascript:emoticon('Very Happy')

Fritz
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forum.vmspython.org Forum Index -> Python for OpenVMS All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group