from pulsar import MethodNotAllowed, spawn, send
from pulsar.apps import wsgi
def set_quantity(actor):
actor.quantity = 10
def decr_quantity(actor, quantity):
actor.quantity -= quantity
return actor.quantity
async def request_handler(environ, start_response):
if environ['REQUEST_METHOD'] == 'GET':
aid = 'actor_123'
await spawn(aid=aid, start=set_quantity)
existing_quantity = await send(aid, 'run', decr_quantity, 1)
data = bytearray('actor1 has {} left\n'.format(existing_quantity).encode())
status = '200 OK'
response_headers = [
('Content-type', 'text/plain'),
('Content-Length', str(len(data)))
]
start_response(status, response_headers)
return iter([data])
else:
raise MethodNotAllowed
def server(description=None, **kwargs):
return wsgi.WSGIServer(request_handler, description=description, **kwargs)
if __name__ == '__main__': # pragma nocover
server().start()
Hitting this gives the following error:
2017-06-29 08:21:58 [p=1077,t=140102986135296] [ERROR] [pulsar.actor] Exception while firing onetime event
Traceback (most recent call last):
File "/virtualenv/allocation/lib/python3.5/site-packages/pulsar/async/events.py", line 133, in _process
result = hnd(arg, exc=exc, **kwargs)
TypeError: set_quantity() got an unexpected keyword argument 'exc'
2017-06-29 08:21:58 [p=1077,t=140102986135296] [INFO] [pulsar.actor] started
actor[actor_123] decr_quantity
2017-06-29 08:21:58 [p=1077,t=140102986135296] [ERROR] [pulsar.actor] Unhandled exception
Traceback (most recent call last):
File "/virtualenv/allocation/lib/python3.5/site-packages/pulsar/async/mailbox.py", line 229, in _on_message
self)
File "/virtualenv/allocation/lib/python3.5/site-packages/pulsar/async/mailbox.py", line 82, in command_in_context
result = cmnd(request, args, kwargs)
File "/virtualenv/allocation/lib/python3.5/site-packages/pulsar/async/proxy.py", line 56, in command_function
return f(request, *args, **kwargs)
File "/virtualenv/allocation/lib/python3.5/site-packages/pulsar/async/commands.py", line 39, in run
return callable(request.actor, *args, **kwargs)
File "/var/www/allocation/allocation_entry.py", line 18, in decr_quantity
actor.quantity -= quantity
AttributeError: 'Actor' object has no attribute 'quantity'
2017-06-29 08:21:58 [p=1063,t=140102986135296] [INFO] [pulsar.wsgi] GET / HTTP/1.1 - 200 OK
I'm struggling to find an example of where `spawn(start=do_something)` is actually used, however it is hinted at in http://quantmind.github.io/pulsar/api/actor.html#spawn
Thanks for any help