-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatch.py
More file actions
70 lines (58 loc) · 1.84 KB
/
Copy pathwatch.py
File metadata and controls
70 lines (58 loc) · 1.84 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 19 23:34:12 2020
@last modified: 29 APR 2020
@author: sheng
@description:
A bunch of process checking functions
Can be used with any other Python script by import
"""
import os
import psutil
import gc
import time
from time import process_time
import sys
import pandas as pd
import numpy as np
#logging.debug("debug")
#logging.info("info")
#logging.warning("warning")
#logging.error("error")
#logging.critical("critical")
##### SYSTEM PERFORMANCE TESTING FUNCTIONS ######
def follow(thefile):
thefile.seek(0,2) # Go to the end of the file
while True:
line = thefile.readline()
if not line:
time.sleep(0.1) # Sleep briefly
continue
yield line
##### SYSTEM PERFORMANCE TESTING FUNCTIONS ######
def memory_usage_psutil():
"""return the memory usage of the process in MB"""
process = psutil.Process(os.getpid())
mem = process.memory_info()[0] / float(2 ** 20)
print("Memory usage of process in MB: ", mem)
return mem
def memory_usage_pandas(df):
"""returns pandas dataframe memory usage in MB"""
mem = df.memory_usage(index=True,deep=True).sum()
print("---------------------------------------------------------------------------------------------")
print("Memory usage of loading csv, parquet, msgpack or hdf file into pandas dataframe in MB: ", mem/8e6)
print("---------------------------------------------------------------------------------------------")
return mem
if __name__=="__main__":
thefile = 'stuff.log'
# line = follow(thefile)
# print(line)
file = open(thefile,'r')
while 1:
where = file.tell()
line = file.readline()
if not line:
time.sleep(1)
file.seek(where)
else:
print(line) # already has newline