Putting it all together

Now, all the drivers and base blocks are available. These elements must be combined together, via a component that is the entry point of the application. Let's define the APIs of this component:

Drivers = namedtuple('Drivers', ['encoder', 'httpd', 'file', 'argv'])
Source = namedtuple('Source', ['encoder', 'httpd', 'file', 'argv'])
Sink = namedtuple('Sink', ['encoder', 'httpd', 'file'])

Four drivers are used in total:

  • The encoder especially implemented for this application
  • The httpd driver from the cyclotron-aio package
  • The file and argv drivers from the cyclotron-std package

Each of these drivers emits source items that are used by the application. So, the Source object also contains the same four fields. However, the argv driver has no sink. So, the Sink object is only composed of three driver sinks.

These objects can now be used to start the application:

def audio_encoder(sources):
...
return Sink(
encoder=encoder.Sink(request=encoder_request),
file=file.Sink(request=file_requests),
httpd=httpd.Sink(control=http),
)


def main():
dispose = run(
entry_point=Component(call=audio_encoder, input=Source),
drivers=Drivers(
encoder=encoder.make_driver(),
httpd=httpd.make_driver(),
file=file.make_driver(),
argv=argv.make_driver(),
)
)
dispose()

The main function uses the run function of the cyclotron-aio package. This call connects the drivers with the entry point function and starts the AsyncIO event loop. The entry point of the application is the audio_encoder function. It returns three sink objects, one for each driver sink. The content of the audio_encoder function is not fully reproduced here, it is the code previously written. The whole code is available in the in the GitHub repository (https://github.com/PacktPublishing/Hands-On-Reactive-Programming-with-Python) of this book.

In order to easily start the server, a wrapper is needed to call the main function. This is done with a shell script, inserted in the script folder of the package:

#!/usr/bin/env PYTHONUNBUFFERED="1" python

from audio_encode_server.server import main

main()

This script is declared in the scripts section of the setup.py file. This allows us to copy it into the binary path of the virtualenv system, and so to execute it from any location in a shell Terminal.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.104.183