Cleaning up after a python script has been run locally vs from Django -
edit: when "global variables" in post, i'm referring mtcbody script; django code doesn't make use of global variables.
i have rather cryptic problem; have python script takes in xml file, parses , returns sort of result. script has global variables uses job. when run script locally, eclipse example, these global variables , else automatically destroyed @ end , new run uses new resources (as expected).
however, when run script django view, expect same behaviour instead, script persists global variables , adds them every time request made.
here's example of running locally eclipse:
import mtcbody def doshit(): context = {} result = mtcbody.getdata() # variable result['tvas_after'] 1 of global variables in mtcbody script print 'tvas after: ' + str(result['tvas_after']) return context if __name__ == '__main__': doshit()
and result after first run:
tvas after: {'1': <mtcbody.tva instance @ 0x7f28011aac20>}
the result after second run:
tvas after: {'1': <mtcbody.tva instance @ 0x7f94bbb96c20>}
the result after third run:
tvas after: {'1': <mtcbody.tva instance @ 0x7f9509f49c20>}
as can see, dictionary holds tva instances repopulated scratch during each run; compare behaviour when running same script django view:
def index(request): context = requestcontext(request) result = mtcbody.getdata() print 'tvas after: ' + str(result['tvas_after']) return render(request, 'dashboardbase/table.html', context)
result after first refresh (request)
tvas after: {'1': <multitvaconfig.mtc.mtcbody.tva instance @ 0x7ff0850fb488>}
result after second refresh (request)
tvas after: {'1': <multitvaconfig.mtc.mtcbody.tva instance @ 0x7ff0850fb488>, '2': <multitvaconfig.mtc.mtcbody.tva instance @ 0x7ff066e4afc8>}
result after third refresh (request)
tvas after: {'1': <multitvaconfig.mtc.mtcbody.tva instance @ 0x7ff0850fb488>, '3': <multitvaconfig.mtc.mtcbody.tva instance @ 0x7ff0a1fa4ea8>, '2': <multitvaconfig.mtc.mtcbody.tva instance @ 0x7ff066e4afc8>}
as can see, variables in script being retained between calls django view. can please explain behaviour , if possible how avoid , make behave if run locally.
thank you
i don't know why find behaviour hard explain. script run once , quits, server application persistent, therefore should not surprising global data persistent within application.
the solution should obvious: don't use global variables. helpful, except in odd cases when do want data persist between calls. in server environment, different users can requesting resource @ different times, global variables dangerous. in case, without seeing relevant functions within mtcbody
it's hard advise, seems should pass around data within module, perhaps via class.
Comments
Post a Comment