Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
4 changed files
with
96 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |