doc/book/admin/pyro.rst
changeset 10491 c67bcee93248
equal deleted inserted replaced
10490:76ab3c71aff2 10491:c67bcee93248
       
     1 .. _UsingPyro:
       
     2 
       
     3 Working with a distributed client (using Pyro)
       
     4 ==============================================
       
     5 
       
     6 In some circumstances, it is practical to split the repository and
       
     7 web-client parts of the application for load-balancing reasons. Or
       
     8 one wants to access the repository from independant scripts to consult
       
     9 or update the database.
       
    10 
       
    11 Prerequisites
       
    12 -------------
       
    13 
       
    14 For this to work, several steps have to be taken in order.
       
    15 
       
    16 You must first ensure that the appropriate software is installed and
       
    17 running (see :ref:`ConfigEnv`)::
       
    18 
       
    19   pyro-nsd -x -p 6969
       
    20 
       
    21 Then you have to set appropriate options in your configuration. For
       
    22 instance::
       
    23 
       
    24   pyro-server=yes
       
    25   pyro-ns-host=localhost:6969
       
    26 
       
    27   pyro-instance-id=myinstancename
       
    28 
       
    29 Connect to the CubicWeb repository from a python script
       
    30 -------------------------------------------------------
       
    31 
       
    32 Assuming pyro-nsd is running and your instance is configured with ``pyro-server=yes``,
       
    33 you will be able to use :mod:`cubicweb.dbapi` api to initiate the connection.
       
    34 
       
    35 .. note::
       
    36     Regardless of whether your instance is pyro activated or not, you can still
       
    37     achieve this by using cubicweb-ctl shell scripts in a simpler way, as by default
       
    38     it creates a repository 'in-memory' instead of connecting through pyro. That
       
    39     also means you've to be on the host where the instance is running.
       
    40 
       
    41 Finally, the client (for instance a python script) must connect specifically
       
    42 as in the following example code:
       
    43 
       
    44 .. sourcecode:: python
       
    45 
       
    46     from cubicweb import dbapi
       
    47 
       
    48     cnx = dbapi.connect(database='instance-id', user='admin', password='admin')
       
    49     cnx.load_appobjects()
       
    50     cur = cnx.cursor()
       
    51     for name in (u'Personal', u'Professional', u'Computers'):
       
    52         cur.execute('INSERT Tag T: T name %(n)s', {'n': name})
       
    53     cnx.commit()
       
    54 
       
    55 Calling :meth:`cubicweb.dbapi.load_appobjects`, will populate the
       
    56 cubicweb registries (see :ref:`VRegistryIntro`) with the application
       
    57 objects installed on the host where the script runs. You'll then be
       
    58 allowed to use the ORM goodies and custom entity methods and views. Of
       
    59 course this is optional, without it you can still get the repository
       
    60 data through the connection but in a roughly way: only RQL cursors
       
    61 will be available, e.g. you can't even build entity objects from the
       
    62 result set.