如何解决运行时错误:不能使用Selenium启动新线程进行Web清理?

2024-10-02 18:26:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经建立了一个脚本来收集产品和他们的细节从许多网站(~120)。“我想在70页之后运行一个新的线程,但它会给我一个新的错误”。我试图寻找解决方案,比如:.clear()我的列表,或者尝试使用系统获取大小()发现内存泄漏,但还没有成功。你知道有什么问题吗?在

详细的错误消息:

Traceback (most recent call last):

File "C:\EGYÉB\PYTHON\PyCharm\helpers\pydev\pydevd.py", line 1741, in <module>
main()

File "C:\EGYÉB\PYTHON\PyCharm\helpers\pydev\pydevd.py", line 1735, in main
globals = debugger.run(setup['file'], None, None, is_module)

File "C:\EGYÉB\PYTHON\PyCharm\helpers\pydev\pydevd.py", line 1135, in run
pydev_imports.execfile(file, globals, locals) # execute the script

File "C:\EGYÉB\PYTHON\PyCharm\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)

File "C:/EGYÉB/PYTHON/Projects/WebScraping/Selenium_scraping.py", line 63, in <module>
soup1 = BeautifulSoup(driver.page_source, 'html.parser')

File "C:\EGYÉB\PYTHON\Projects\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 679, in page_source
return self.execute(Command.GET_PAGE_SOURCE)['value']

File "C:\EGYÉB\PYTHON\Projects\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 319, in execute
response = self.command_executor.execute(driver_command, params)

File "C:\EGYÉB\PYTHON\Projects\venv\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 374, in execute
return self._request(command_info[0], url, body=data)

File "C:\EGYÉB\PYTHON\Projects\venv\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 423, in _request
data = utils.load_json(data.strip())

File "C:\EGYÉB\PYTHON\Projects\venv\lib\site-packages\selenium\webdriver\remote\utils.py", line 37, in load_json
return json.loads(s)

File "C:\EGYÉB\PYTHON\Python Core\lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)

File "C:\EGYÉB\PYTHON\Python Core\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())

File "C:\EGYÉB\PYTHON\Python Core\lib\json\decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
MemoryError

Traceback (most recent call last):
File "C:\EGYÉB\PYTHON\PyCharm\helpers\pydev\_pydevd_bundle\pydevd_comm.py", line 1505, in do_it
t.start()

File "C:\EGYÉB\PYTHON\Python Core\lib\threading.py", line 847, in start
_start_new_thread(self._bootstrap, ())
RuntimeError: can't start new thread

代码:

^{pr2}$

Tags: inpyselfjsonremoteliblinehelpers
1条回答
网友
1楼 · 发布于 2024-10-02 18:26:19

此错误消息。。。在

RuntimeError: can't start new thread

…意味着系统“无法启动新线程”,因为在python进程中已经有太多线程在运行,并且由于资源限制,创建新线程的请求被拒绝。在

你的主要问题来自于这一行:

^{pr2}$

您需要查看程序正在创建的线程数与系统能够创建的最大线程数(具体取决于您的环境)。可能你的程序启动的线程比你的系统能处理的线程还要多。对于一个进程,可以激活的线程数是有限制的。在

另一个因素可能是,您的程序启动线程的速度比线程运行到完成的速度快。如果需要启动多个线程,则需要以一种更可控的方式来启动它,那么可以使用线程池。在

考虑到线程是异步运行的,重新设计程序流将是一种更好的方法。也许使用一个线程池来获取资源,同时为每个请求启动一个线程。在

您可以找到有关error: can't start new thread的详细讨论

在这里,您还可以找到关于Is there any way to kill a Thread?的详细讨论

相关问题 更多 >