## Spawned Backends

## Introduction

If you do not have a C or C++ language binding, PackageKit executes helper scripts written in pretty much any language. It then watches the standard out and standard error and parses the output into compiled backend commands. This means a python library can be interfaced easily with a C backend.

Even when using helpers, a compiled backend stub is still used for two reasons:

- It is still needed for the dlopen internally in PackageKit.

- You can add cleverness in the C backend that you might not want to do in the scripted backend, for example using a hash table in C rather than checking all the names in awk.

Backends are typically open-programmable, which means we can define a standard for what goes on stdin and stdout to try and maximise the common code between the backends. The following section will explain the convention used on `backends/conary` and `backends/yum`.

If you are unable to write scripts that conform to these specifications then just launch a PkSpawn object in the compiled helper with stdout callbacks and then try to do screenscraping in the backend. This screen-scraping is least popular for obvious reasons.

Backends scripts are run with arguments and data is sent to standard out and standard error asynchronously so that PackageKit can proxy this to DBUS. A method has command line arguments separated with tabs, and data is also separated with tabs.

It is important to flush the standard output after each output, else Linux will helpfully buffer the output into more efficient size chunks. If you do not flush, then there will be a long IPC delay. Flushing can be achieved in C using `fflush` or in python using `sys.stdout.flush()`.

There are two modes of operation for a spawned backend, a one shot mode and a dispatcher mode.

- The "one-shot" mode is where commands are executed (e.g. `search-name.py none power`), which then initialises the package backend, returns results and then exits. This is obviously not ideal, as some backends like smart can take several hundred ms to initialise, and this leads to a "laggy" GUI applications.

  This however is the easiest mode to implement, and the original one supported by all versions of PackageKit.

- The "dispatcher" mode is where a command is used to startup the backend, for instance `yumBackend.py search-name none power` and then the backend then sits and waits for more standard input. Further operations can be done on the loaded backend sending commands to stdin, e.g. `search-name none power`. If there are no more operations after a preset time (default 5 seconds) then the backend is sent `exit` over stdin, and the backend terminates. The daemon will ensure the operations are serialised, and that backends not sending `finished` are cleaned up properly.

  This is quite easy to implement (see the end of yumBackend.py), and is supported from PackageKit version 0.3.2. The dispatcher mode does not have to implemented in python; any language that can read from stdin can block and be used in this way.