Tuesday, July 19, 2016

Setting up Celery, python3, Celery flower and rabbitmq on mac 10.9.5

The following are the steps required to install celery, flower and rabbitmq on a mac 10.9.5 


Install python3

> brew install python3
> python3 -V # should display the version

Install and run rabbitmq

> brew install rabbitmq
> sudo rabbitmq-server # should run the rabbitmq server

# if the rabbitmq-server returns an error like “not_a_dets_file”, then delete the dets file. This is usually due to incorrect shutdown of the rabbitmq-server

> rm /usr/local/var/lib/rabbitmq/mnesia/rabbit\@localhost/recovery.dets

RabbitMQ can now be accessed on localhost:15672

Install celery 

> brew install celery
> mkdir & cd celery_tasks
> vim tasks.py

from celery import Celery

# first arg is the name to be prepended to the tasks
# backend param is an optional tag to retrieve the status of a backrgound running task
# broker is the broker service that routes the incoming and the outgoing messages
app = Celery('tasks', backend='amqp', broker='amqp://')

# all tasks must be decorated
@app.task(ignore_result=True)
def print_hello():
    print ('hello there')


# a task to generate prime numbers that should take time
@app.task
def gen_prime(x):
    multiples=[]
    results=[]
    for i in range(2,x+1):
        if i not in multiples:
            results.append(i)
            for j in range(i*i,x+1,i):
                multiples.append(j)
    return results

> celery worker -A tasks &

- To run the actual tasks
> python3
> from tasks import print_hello
> from tasks import gen_prime
> print_hello()
> primes=gen_prime(1000)
> print primes
> # to check if the task is complete
> primes.ready()
> # to get the results
> print primes.get()

Install Celery flower 

> pip3 install flower
> flower —port 5555
> # accessing flower on the port should show the list of active tasks

Celery flower can now be accessed on localhost:5555

Interesting links 

https://www.digitalocean.com/community/tutorials/how-to-use-celery-with-rabbitmq-to-queue-tasks-on-an-ubuntu-vps

Friday, April 8, 2016

Setting up Octave environment to calculate laplace() and ilaplace()

I recently encountered an issue trying to calculate the laplace() and inverse laplace() transforms using Octave. Here are the set of steps that I used to set up support for these functions in octave:

My environment:

>> lsb_release -a 
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 14.04.4 LTS
Release: 14.04
Codename: trusty

>> sudo aptitude install octave # install octave 
>> octave -version 
GNU Octave, version 3.8.1
Copyright (C) 2014 John W. Eaton and others.
This is free software; see the source code for copying conditions.
There is ABSOLUTELY NO WARRANTY; not even for MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.

Octave was configured for "x86_64-pc-linux-gnu".

Additional information about Octave is available at http://www.octave.org.

Please contribute if you find this software useful.
For more information, visit http://www.octave.org/get-involved.html

Read http://www.octave.org/bugs.html to learn how to submit bug reports.


>> sudo aptitude install liboctave-dev # install octave-dev libs 
>> sudo apt-get install python-setuptools
>> sudo easy_install pip
>> sudo pip install sympy
>> octave --force-gui # install octave in gui mode 

Octave should launch with the gui loaded: 


Run the following commands in Octave: 

Octave$>> pkg install -forge symbolic
This command should take a while to load and the following error message should be displayed 
For information about changes from previous versions of the symbolic package, run 'news symbolic'.

Octave$>> news symbolic # to test that symbolic is installed 



Octave$>> pkg load symbolic  # load symbolic package 

You should now be able to run laplace and inverse laplace transforms as shown in the screen shot below

Octave$>> syms t
>> f = t^2;
>> laplace(f)
ans = (sym)

  2
  ──
   3
  s

>> syms t s
>> f=-1.25+3.5*t*exp(-2*t)+1.25*exp(-2*t);
warning: Using rat() heuristics for double-precision input (is this what you wanted?)
warning: Using rat() heuristics for double-precision input (is this what you wanted?)
warning: Using rat() heuristics for double-precision input (is this what you wanted?)
>> F=laplace(f,t,s)
F = (sym)

       s - 5
  ────────────────
     ⎛ 2               ⎞
  s⋅⎝s  + 4⋅s + 4⎠

>> syms a s x
>> F = 1/(s - a)^2;
>> ilaplace(F, x)
ans = (sym)

     x⋅re(a)  ⅈ⋅x⋅im(a)
  x⋅ℯ       ⋅ℯ