[크롤링/16]메시지 큐(파이썬 Redis)
- 크롤링과 스크레이핑을 분리해서 비동기적으로 처리하자
우선 크롤링과 스크레이핑은 다르다. 크롤링은 HTML/파일 등을 추출하고 저장하는 것이고, 스크레이핑은 그 결과물인 HTML에서 원하는 데이터를 추출하는 것이다.
그리고 일반적으로 크롤링보다 스크레이핑에서 문제가 더 많이 생긴다. 실제로 회사에서 크롤러를 관리할 때도 크롤링에 실패한 경우 원인을 파악해보면 십중팔구 스크레이핑이 문제였다.
크롤링은 HTML을 긁어오기만 하면 된다. 그 HTML에서 키워드 등 원하는 데이터의 유무를 판단하는 건 스크레이핑의 영역이다. HTML을 긁어오는데서 실패를 할 가능성은 웹 서버 에러를 제외하고는 거의 없지만, 스크레이핑은 데이터의 위치가 바뀌었다던지, 데이터가 더이상 포함되어있지않다던지 다양한 실패 가능성이 있다.
그래서 크롤링과 스크레이핑을 분리해서 처리하기도 한다. 이렇게 했을때의 장점은 '크롤링을 성공한 후 스크레이핑을 실패했을 때, 다시 크롤링을 하지 않아도 된다'라는 것이다. 이미 저장한 HTML에서 다시 스크레이핑을 하면 되기 때문이다.
하지만 단점으로는 관리가 복잡해지고, 스크레이핑을 할 때까지는 크롤링에서 문제가 생긴 것을 모를 수도 있다는 점이 되겠다.
크롤링 소요 시간이 짧다면 크롤링이 모두 완료된 후 스크레이핑을 해도 되지만, 크롤링 소요 시간이 길다면 두 처리를 연결하는 방법이 필요하다. 두 처리를 연결하는데 사용이 되는 도구가 메시지 큐다.
- 메시지 큐
"Message queues implement an asynchronous communication pattern between two or more processes/threads whereby the sending and receiving party do not need to interact with the message queue at the same time.
메시지 큐는 두 개 이상의 프로세스/스레드 간에 비동기 통신 패턴을 구현하며, 이 경우 송신 당사자와 수신 당사자는 동시에 메시지 큐와 상호 작용할 필요가 없다.
(1) RQ+Redis
RQ 다운로드: python-rq.org/
RQ: Simple job queues for Python
RQ (Redis Queue) is a simple Python library for queueing jobs and processing them in the background with workers. It is backed by Redis and it is designed to have a low barrier to entry. It can be integrated in your web stack easily. RQ requires Redis >= 3
python-rq.org
Redis 다운로드: redis.io/download
Redis
*Download Stable releases liberally follow the usual major.minor.patch semantic versioning schema. *Other versions Old (6.0) Redis 6.0 introduces SSL, the new RESP3 protocol, ACLs, client side caching, diskless replicas, I/O threads, faster RDB loading, ne
redis.io
RQ (Redis Queue)는 작업을 대기열에 추가하고 작업자와 함께 백그라운드에서 처리하는 파이썬 라이브러리다. Redis가 설치되어 있어야하고, 웹 스택에 쉽게 통합 할 수 있다.
현재는 Redis 버전 3.0.0이상을 요구한다.
pip install rq
Redis는 맥과 우분투에서는 쉽게 설치할 수 있지만, 윈도우즈에서는 공식적으로 지원되지 않는다. 하지만 마이크로소프트가 지원해주기 때문에 아래의 링크를 따라가면 설치할 수 있다.
Windowns Redis 다운로드: github.com/microsoftarchive/redis
microsoftarchive/redis
Redis is an in-memory database that persists on disk. The data model is key-value, but many different kind of values are supported: Strings, Lists, Sets, Sorted Sets, Hashes - microsoftarchive/redis
github.com
(2) Celery+RabbitMQ
Celery 다운로드 : docs.celeryproject.org/en/stable/
Celery - Distributed Task Queue — Celery 5.0.5 documentation
This document describes the current stable version of Celery (5.0). For development docs, go here.
docs.celeryproject.org
Celery는 방대한 양의 메시지를 처리하는 간단하고 유연하며 신뢰할 수있는 분산 시스템이며 이러한 시스템을 유지하는 데 필요한 도구를 제공한다.
파이썬 버전에 따른 셀러리 버전은 아래와 같다.
- Python 2.7 or Python 3.5: Celery series 4.4 or earlier.
- Python 2.6: Celery series 3.1 or earlier.
- Python 2.5: Celery series 3.0 or earlier.
- Python 2.4 was Celery series 2.2 or earlier.
RabbitMQ 다운로드: www.rabbitmq.com/download.html
Downloading and Installing RabbitMQ — RabbitMQ
Downloading and Installing RabbitMQ The latest release of RabbitMQ is 3.8.14. See change log for release notes. See RabbitMQ support timeline to find out what release series are supported. Experimenting with RabbitMQ on your workstation? Try the community
www.rabbitmq.com
RabbitMQ 튜토리얼: www.rabbitmq.com/getstarted.html
RabbitMQ Tutorials — RabbitMQ
RabbitMQ Tutorials These tutorials cover the basics of creating messaging applications using RabbitMQ. You need to have the RabbitMQ server installed to go through the tutorials, please see the installation guide or use the Docker image. Executable version
www.rabbitmq.com
RabbitMQ는 가장 인기있는 오픈 소스 메시지 브로커 중 하나다. RabbitMQ는 가볍고 온 프레미스 및 클라우드에 배포하기 쉽고 여러 메시징 프로토콜을 지원한다.