Regarding memory consumption in python with urllib2 module

PS: I have a similar question with HTTP library requests here .

I am using python v2.7 for windows 7 OS. I am using urllib2 module. I have two pieces of code. One file is called myServer.py The server class has 2 methods named getName(self,code) and getValue(self) . Another script, called testServer.py , simply calls methods from the server class to retrieve the values โ€‹โ€‹and print them. The server class basically retrieves values โ€‹โ€‹from a server on my local network. Therefore, unfortunately, I canโ€™t give you access to test the code.

Problem . When I execute my testServer.py file, I observed in the task manager that memory consumption continued to grow. Why is it growing and how to avoid it? If I comment on the next line

 print serverObj.getName(1234) 

in testServer.py , that is, an increase in no memory consumption.

I am sure that the problem is with getName(self,code) of server class. But, unfortunately, I could not understand what the problem was.

Code: Below you will find the code snippets:

 #This is the myServer.py file import urllib2 import json import random class server(): def __init__(self): url1 = 'https://10.0.0.1/' username = 'user' password = 'passw0rd' passwrdmgr = urllib2.HTTPPasswordMgrWithDefaultRealm() passwrdmgr.add_password(None, url1, username, password) authhandler = urllib2.HTTPBasicAuthHandler(passwrdmgr) opener = urllib2.build_opener(authhandler) urllib2.install_opener(opener) def getName(self, code): code = str(code) url = 'https://10.0.0.1/' + code response = urllib2.urlopen(url) data = response.read() name = str(data).strip() return name def getValue(self): value = random.randrange(0,11) return value 

Below is a snippet of testServer.py

 from myServer import server import time serverObj = server() while True: time.sleep(1) print serverObj.getName(1234) print serverObj.getValue() 

Thank you for your time!

+1
source share
1 answer

This question is very similar to my other question. Therefore, I think the answer is also very similar. The answer can be found here fooobar.com/questions/966558 / ...

0
source

All Articles