Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

# encoding: utf-8 

 

import os 

import time 

 

 

class InWriteException(Exception): 

pass 

 

 

def wait_for_read(path, max_time=5, poll_time=.05): 

"""waits up to max_time seconds if size of file provided by "path" changes. We check 

the size every "poll_time" seconds for this. 

 

If file size does not change during the interval we may assume that no other processes 

write to the file any more. 

""" 

 

def size(): 

return os.stat(path).st_size 

 

started = time.time() 

previous_size = size() 

 

while time.time() <= started + max_time: 

time.sleep(poll_time) 

new_size = size() 

size_change = new_size - previous_size 

if size_change == 0: 

break 

previous_size = new_size 

else: 

raise InWriteException("gave up after {} seconds, file writing not finished yet".format(max_time))