Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Changing factorial example to have multiple calls. (#228)
* Changing factorial example to have multiple calls.

* Parallel downloading with requests using Worker and task.

* Adding Class and method name to help find method causing issues.
It was silent otherwise and impossible to find by error message.
Is this style of except Python 2.7 compatible?

* Changing to allow running on Windows platforms.
AF_UNIX does not exist in Windows (3.5)
  • Loading branch information
sacherjj authored and prologic committed Apr 19, 2018
1 parent ecfbe00 commit 9f8306c
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 2 deletions.
5 changes: 4 additions & 1 deletion circuits/core/handlers.py
Expand Up @@ -123,4 +123,7 @@ def __init__(cls, name, bases, ns):
callables = (x for x in ns.items() if isinstance(x[1], Callable))
for name, callable in callables:
if not (name.startswith("_") or hasattr(callable, "handler")):
setattr(cls, name, handler(name)(callable))
try:
setattr(cls, name, handler(name)(callable))
except ValueError as e:
raise ValueError('{} - {} {}'.format(str(e), repr(cls), name))
7 changes: 6 additions & 1 deletion circuits/net/sockets.py
Expand Up @@ -10,11 +10,16 @@
EMFILE, ENFILE, ENOBUFS, ENOMEM, ENOTCONN, EPERM, EPIPE, EWOULDBLOCK,
)
from socket import (
AF_INET, AF_INET6, AF_UNIX, IPPROTO_IP, IPPROTO_TCP, SO_BROADCAST,
AF_INET, AF_INET6, IPPROTO_IP, IPPROTO_TCP, SO_BROADCAST,
SO_REUSEADDR, SOCK_DGRAM, SOCK_STREAM, SOL_SOCKET, TCP_NODELAY,
error as SocketError, gaierror, getaddrinfo, getfqdn, gethostbyname,
gethostname, socket,
)
try:
from socket import AF_UNIX
except ImportError:
AF_UNIX = None

from time import time

from _socket import socket as SocketType
Expand Down
40 changes: 40 additions & 0 deletions examples/async_worker_webpage_download.py
@@ -0,0 +1,40 @@
from time import sleep
from circuits import Component, Debugger, Event, Timer, Worker, task
import requests


def download_web_page(url):
print('Downloading {}'.format(url))
response = requests.get(url)
sleep(2) # This website is really slow.
# Only returning portion of web page.
# You would probably process web page for data before sending back
return response.text[:200]

class App(Component):

def init(self, *args, **kwargs):
self.foo_count = 0
Worker(process=False).register(self)

def foo(self):
self.foo_count += 1
print("Foo!")
if self.foo_count > 10:
self.stop()

def started(self, component):
# x = yield self.call(task(factorial, 10))
Timer(1, Event.create("foo"), persist=True).register(self)
self.fire(task(download_web_page, 'http://www.slickdeals.net')) # async
self.fire(task(download_web_page, 'http://www.google.com')) # async
self.fire(task(download_web_page, 'http://www.yahoo.com')) # async

def task_success(self, function_called, function_result):
func, url_called = function_called
print('url {} gave {}'.format(url_called, function_result))

if __name__ == '__main__':
app = App()
Debugger().register(app)
app.run()
46 changes: 46 additions & 0 deletions examples/factorial_multiple.py
@@ -0,0 +1,46 @@
#!/usr/bin/env python
from __future__ import print_function

from time import sleep

from circuits import Component, Debugger, Event, Timer, Worker, task


def factorial(n):
x = 1
for i in range(1, (n + 1)):
x = x * (i + 1)
sleep(1) # deliberate!
return x


class App(Component):

def init(self, *args, **kwargs):
Worker(process=True).register(self)

def foo(self):
print("Foo!")

def started(self, component):
self.fire(task(factorial, 3)) # async
self.fire(task(factorial, 5)) # async
self.fire(task(factorial, 7)) # async
self.fire(task(factorial, 10)) # async
self.fire(task(factorial, 11)) # async
self.fire(task(factorial, 11)) # async
self.fire(task(factorial, 12)) # async
self.fire(task(factorial, 14)) # async
Timer(1, Event.create("foo"), persist=True).register(self)

def task_success(self, function_called, factorial_result):
func, argument = function_called
print("factorial({0}) = {1:d}".format(str(argument), factorial_result))
# Stop after the last and longest running task
if argument == 14:
self.stop()

if __name__ == '__main__':
app = App()
Debugger().register(app)
app.run()

0 comments on commit 9f8306c

Please sign in to comment.