multithreading - Python gevent threads not releasing memory -
i using gevent.monkey.patch_all() , import threading.
from threading importing thread , creating new thread that:
thread = thread(target=handler, args=args) thread.start()
then executing code path multiple times , can see memory growing , never being released. may taking hours release not okay well.
however if directly handler(args) don't memory increase.
do need kill thread how? or stop it?
thanks
if correct, think want put task @ background
, execute periodically or event driven through out life time. process initiate background task exist in memory right ?
you can use gevent.spwan
implement this. actual co-routine
.
background.py :-
import gevent import gevent.monkey import datetime import time gevent.monkey.patch_all() def my_task(*args, **kwargs): open("/tmp/test.log", "a+") f: f.write("time: {}\n".format(datetime.datetime.now())) def exec_background(task, *args, **kwargs): """ run `task` background given `args` , `kwargs`. """ def run_periodically(*args, **kwargs): while 1: gevent.sleep(1) task(*args, **kwargs) return gevent.spawn(run_periodically, *args, **kwargs) if __name__ == "__main__": exec_background(my_task) print "waiting... other stuffs here !" time.sleep(100)
this way can reuse exec_background
method putting different tasks @ background.
$ python background.py waiting... other stuffs here !
check file tail -f /tmp/test.log
can see contents appending in background.
regarding on memory usage, think related task. might doing lot of network operations, make sure cleaning stuffs properly. python memory manager garbage collection of time, don't think thing wrong that.
check task logic entire lifetime of app , chances accumulating resources sockets or objects. use python context managers :).
hope helpful you.
Comments
Post a Comment