【问题标题】:how can i manually manage memory in python?如何在 python 中手动管理内存?
【发布时间】:2017-12-24 15:58:04
【问题描述】:

当我并行运行我的程序(网络爬虫)时,它通过我的系统需要异常数量的内存或内存,我还用其他网络爬虫进行了测试,我的网络爬虫占用的内存是它们的两倍,所以我的问题是 如何在 python 中手动管理内存或 ram,(如果可能)?

这是我的代码:-

from bs4 import BeautifulSoup
import requests
import MySQLdb as sql
import time
import warnings

print("starting")

warnings.filterwarnings('ignore')

db = sql.connect("localhost", "root", "arpit", "website")
cursor = db.cursor()
db.autocommit(True)

print("connected to database")

url = "http://www.example.com"
extension = ".com"
print("scrapping url -",url)

r = requests.head(url)
cursor.execute("insert ignore into urls(urls,status,status_code)     
values(%s,'pending',%s)", [url, r.status_code])

cursor.execute("select status from urls where status ='pending' limit 1")
result = str(cursor.fetchone())

while (result != "None"):

cursor.execute("select urls from urls where status ='pending' limit 1")
result = str(cursor.fetchone())

s_url = result[2:-3]

cursor.execute("update urls set status = 'done' where urls= %s ", [s_url])

if "https" in url:
    url1 = url[12:]
else:
    url1 = url[11:]
zone = 0
while True:

    try:
        r = requests.get(s_url,timeout=60)
        break

    except:
        if s_url == "":

            print("done")
            break
        elif zone >= 4:
            print("this url is not valid -",s_url)
            break
        else:
            print("Oops!  may be connection was refused.  Try again...",s_url)
            time.sleep(0.2)
            zone = zone + 1

soup = BeautifulSoup(r.content.lower(), 'lxml')

links = soup.find_all("a")

for x in links:
    a = x.get('href')
    if a is not None and a != "":

        if a != "" and a.find("\n") != -1:
            a = a[0:a.find("\n")]

        if a != "" and a[-1] == "/":
            a = a[0:-1]

        if a != "":
            common_extension = [',',' ',"#",'"','.mp3',"jpg",'.wav','.wma','.7z','.deb','.pkg','.rar','.rpm','.tar','.zip','.bin','.dmg','.iso','.toast','.vcd','.csv','.dat','.log','.mdb','.sav','.sql','.apk','.bat','.exe','.jar','.py','.wsf','.fon','.ttf','.bmp','.gif','.ico','.jpeg','.png','.part','.ppt','.pptx','.class','.cpp','.java','.swift','.ods','.xlr','.xls','.xlsx','.bak','.cab','.cfg','.cpl','.dll','.dmp','.icns','.ini','.lnk','.msi','.sys','.tmp','.3g2','.3gp','.avi','.flv','.h264','.m4v','.mkv','.mov','.mp4','.mpg','.vob','.wmv','.doc','.pdf','.txt']
            for ext in common_extension:
                if ext in a:
                    a = ""
                    break

        if a != "":
            if a[0:5] == '/http':
                a = a[1:]
            if a[0:6] == '//http':
                a = a[2:]

            if a[0:len(url1) + 12] == "https://www." + url1:
                cursor.execute("insert ignore into urls(urls,status,status_code) values(%s,'pending',%s)",
                               [a, r.status_code])
            elif a[0:len(url1) + 11] == "http://www." + url1:
                cursor.execute("insert ignore into urls(urls,status,status_code) values(%s,'pending',%s)",
                               [a, r.status_code])
            elif a[0:len(url1) + 8] == "https://" + url1:
                cursor.execute("insert ignore into urls(urls,status,status_code) values(%s,'pending',%s)",
                               [url + (a[(a.find(extension + "/")) + 4:]), r.status_code])
            elif a[0:len(url1) + 7] == "http://" + url1:
                cursor.execute("insert ignore into urls(urls,status,status_code) values(%s,'pending',%s)",
                               [url + (a[(a.find(extension + "/")) + 4:]), r.status_code])
            elif a[0:2] == "//" and a[0:3] != "///" and "." not in a and "http" not in a and "www." not in a:
                cursor.execute("insert ignore into urls(urls,status,status_code) values(%s,'pending',%s)",
                               [url + a[1:], r.status_code])
            elif a[0:1] == "/" and a[0:2] != "//" and "." not in a and "http" not in a and "www." not in a:
                cursor.execute("insert ignore into urls(urls,status,status_code) values(%s,'pending',%s)",
                               [url + a[0:], r.status_code])
            elif 'http' not in a and 'www.' not in a and "." not in a and a[0] != "/":
                cursor.execute("insert ignore into urls(urls,status,status_code) values(%s,'pending',%s)",
                               [url + '/' + a, r.status_code])

cursor.execute("alter table urls drop id")
cursor.execute("alter table urls add id int primary key not null  
auto_increment first")
print("new id is created")

【问题讨论】:

  • 这不是有效的 Python。您的缩进不正确,这意味着不清楚发生了什么。你能修复你的缩进吗?您将 RAM 使用情况分析到什么级别?您是检查单个线程还是仅检查整体使用情况?您是否将您的数据库包括在内?
  • 顺便说一句,如果你想高效爬取,试试看scrapy

标签: python python-3.x python-2.7 web-crawler


【解决方案1】:

您的代码的内存效率非常低,因为您正在执行很多切片 - 并且因为字符串是不可变的,每个切片都会分配一个新对象。

例如:

if a[0:5] == '/http'
   a = a[1:]

分配一个新字符串,将a0复制到5,与'/http'比较,然后丢弃;此外,如果它测试相等,它会分配一个新字符串,将a1 复制到它上面,然后将a 扔掉。如果a 很长,或者这种情况经常发生,这可能会成为一个很大的问题。

查看memoryviews - 这是一种在不复制字符串的情况下对字符串(嗯,Python 3 中的bytes)进行切片的方法。

还有很多其他方法可以优化您的代码:

  1. 不要为每个链接重新定义common_extension,而是在循环之前定义一次。

  2. 使用a.startswith('/http'),而不是a[0:5] == '/http'

  3. 不要使用前 4 个 url1 比较,而是使用像 re.match('https?://(www\.)?' + re.escape(url1), a) 这样的正则表达式。

    如果您这样做,不要为每个链接连接'https?://(www\.)?'re.escape(url1),而是在循环之前执行一次,甚至是re.compile 那里的正则表达式。

【讨论】:

    【解决方案2】:

    您不能在 Python 中直接管理内存,因为除了创建和删除对象之外,您无法分配和/或释放内存块。您可以做的是使用工具来了解您的代码的哪些部分使用了多少内存。有关详细信息,请参阅例如https://www.pluralsight.com/blog/tutorials/how-to-profile-memory-usage-in-python

    在 python 中,限制内存使用的方法显然是不要创建大型对象列表或其他数据结构。使用回调、产量和/或其他编码实践来限制结构在内存中花费的时间。

    我还建议将您的代码提交给代码审查 SE,我认为他们也可以提供帮助。

    【讨论】:

      【解决方案3】:

      简短的回答是,您不能手动管理内存。但是,您的问题确实应该是,我如何减少 Python 使用的内存量?

      首先,了解您的程序分配的内存量与它使用的内存量之间存在差异。

      其次,避免分配你不需要的内存。每个切片操作都会创建一个新的str 对象。相反,使用startswith 方法:

      if not a:
          if a.startswith('/http'):
              a = a[1:]
          if a.startswith('//http'):
              a = a[2:]
      
          if a[.startswith("https://www." + url1):
              cursor.execute("insert ignore into urls(urls,status,status_code) values(%s,'pending',%s)",
                             [a, r.status_code])
      

      等等

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-07-29
        • 1970-01-01
        • 2014-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多