| 76 | | The most simple way to run your service is using the <code class="shell">twistd |
| 77 | | run</code> plugin. It allows your application to put the service wherever |
| 78 | | you want, and it doesn't have to be a plugin. Thus, the interface to provide |
| 79 | | is simple: it just needs an <code>options</code> attribute and a |
| 80 | | <code>makeService</code>method. Let's imagine a directory structure: |
| | 76 | The simplest way to run your service is using the <code class="shell">twistd |
| | 77 | run</code> command. It allows you to use <code class="shell">twistd</code> to |
| | 78 | start a service with a minimum of coding overhead. The interface to provide |
| | 79 | to use this is simple: just provide <code>options</code> and <code>makeService |
| | 80 | </code>. Imagine the following directory structure: |
| 128 | | The <code>Options</code> class defines the options your service needs. The |
| 129 | | <code>longdesc</code> attribute will be printed when you'll call <code |
| 130 | | class="shell">--help</code> on you service. The <code>MyServiceMaker</code> |
| 131 | | implements the <code>ISimpleServiceMaker</code> interface, which needs an |
| 132 | | <code>options</code> attribute and a <code>makeService</code> method. This |
| 133 | | method is called with an <code>options</code> argument, instance of the |
| 134 | | <code>option</code> attribute filled with the data passed at command line. |
| 135 | | It returns a service you want to run, here a |
| 136 | | <code>internet.TCPServer</code>. Note that you need to create an instance |
| 137 | | of <code>MyServiceMaker</code>: <code class="shell">twistd shell</code> |
| 138 | | takes an object that <strong>provides</strong> |
| 139 | | <code>ISimpleServiceMaker</code>, not just implements it. |
| | 129 | <code class="python">MyServiceMaker</code> implements <code class="API" |
| | 130 | base="twisted.application.service">ISimpleServiceMaker</code>, providing |
| | 131 | two important attributes. First, <code class="python">options</code> is a |
| | 132 | callable which returns a new option parsing object. This object will |
| | 133 | have <code class="python">parseOptions</code> called on it with a list of |
| | 134 | arguments from the command line. Afterwards, the object will be passed as |
| | 135 | the only argument to <code class="python">MyServiceMaker.makeService |
| | 136 | </code>. <code class="python">makeService</code> is to return the <code |
| | 137 | class="API" base="twisted.application.service">IService</code> |
| | 138 | implementation which will be run by <code class="shell">twistd</code>, |
| | 139 | in this case an <code class="API" base="twisted.application"> |
| | 140 | internet.TCPServer</code>. Note that you need to create an instance of |
| | 141 | <code class="python">MyServiceMaker</code> because <code class="shell"> |
| | 142 | twistd run</code> accepts the name of an object that <strong>provides |
| | 143 | </strong> <code class="API" base="twisted.application.service"> |
| | 144 | ISimpleServiceMaker</code>, not the name of an object that <strong> |
| | 145 | implements</strong> it. |
| 152 | | If you want to be even straightforward, it's important to know that a |
| 153 | | module can provide the <code class="python">ISimpleServiceMaker</code> |
| 154 | | interface. So if you put the following code instead in service.py: |
| | 159 | It's also possible for a module to provide <code class="API" |
| | 160 | base="twisted.application.service">ISimpleServiceMaker</code>. This |
| | 161 | is useful if the implementation is stateless or multiple instances |
| | 162 | are not required. For example, the above service.py could be rewritten |
| | 163 | like this: |
| 191 | | plugin, <code class="shell">twistd</code> will list you every plugin it can |
| 192 | | found. It also supports a description that will be printed when you call |
| 193 | | <code class="shell">twistd --help</code> .The following directory structure |
| 194 | | is assumed of your project:</p> |
| | 203 | plugin, <code class="shell">twistd</code> can present a list of every |
| | 204 | plugin available. Plugins also add support for descriptions which will |
| | 205 | be included in the list presented by <code class="shell">twistd --help |
| | 206 | </code> .The following directory structure is assumed of your project: |
| | 207 | </p> |
| | 208 | |