Last active
January 10, 2018 05:33
-
-
Save mortymacs/ddbf60b227a1f5509a9f5f6e2dcd1e6d to your computer and use it in GitHub Desktop.
Simple Actor Model in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import threading | |
from queue import Queue, Empty | |
class Actor(object): | |
"""Basic event-based actor abstract.""" | |
def __init__(self): | |
"""Initialize Actor class.""" | |
self.inbox = Queue() | |
self.setup() | |
def receive(self, message): | |
"""Receive message and do action.""" | |
raise NotImplemented() | |
def setup(self): | |
"""Override setup method if required.""" | |
pass | |
def start(self): | |
"""Start receive message.""" | |
self.start = True | |
while self.start: | |
if not self.inbox.empty(): | |
try: | |
message = self.inbox.get_nowait() | |
threading.Thread(target=self.receive, args=(message,)).start() | |
except Empty: | |
pass | |
sleep(0.2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from lxml import html | |
class FetchSiteTitle(Actor): | |
"""Fetch website title.""" | |
def setup(self): | |
"""Override setup method.""" | |
self._db_actor = StoreDataActor() | |
def _fetch_content(self, url): | |
"""Get all content of url.""" | |
try: | |
file = requests.get(url) | |
return file.content | |
except: | |
return None | |
def _fetch_title(self, url): | |
"""Parse content and fetch title tag text.""" | |
content = self._fetch_content(url) | |
if not content: | |
return None | |
html_obj = html.fromstring(content) | |
title_tags = html_obj.xpath("/html/head/title") | |
if title_tags: | |
return title_tags[0].text | |
return None | |
def receive(self, message): | |
"""Implement fetch title receive method.""" | |
url = message.get("url") | |
title = self._fetch_title(url) | |
if title: | |
self._db_actor.inbox.put({"title":title}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if __name__ == "__main__": | |
action = FetchSiteTitle() | |
action.start() | |
action.inbox.put({"url": "http://mortezana.com"}) | |
action.inbox.put({"url": "https://google.com"}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlite3 | |
class StoreDataActor(Actor): | |
"""Store data in database.""" | |
def setup(self): | |
"""Override setup method.""" | |
self._db = sqlite3.connect(":memory") | |
self._cur = self._db.cursor() | |
def receive(self, message): | |
"""Implement store in db receive method.""" | |
title = message.get("title") | |
self._cur.execute("INSERT INTO site_info(title) VALUES ({})".format(title) | |
self._con.commit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great reference: https://medium.com/@ianjuma/the-actor-model-in-python-with-gevent-b8375d0986fa