What are Python web and worker roles?

Azure provides three compute models for running applications: Web Apps feature in Azure App Service, Azure Virtual Machines, and Azure Cloud Services. All three models support Python. Cloud Services, which include web and worker roles, provide Platform as a Service (PaaS). Within a cloud service, a web role provides a dedicated Internet Information Services (IIS) web server to host front end web applications, while a worker role can run asynchronous, long-running, or perpetual tasks independent of user interaction or input.

Looking to build a simple website?
If your scenario involves just a simple website front end, consider using the lightweight Web Apps feature in Azure App Service. You can easily upgrade to a Cloud Service as your website grows and your requirements change. See the Python Developer Center for articles that cover development of the Web Apps feature in Azure App Service.

Project creation

In Visual Studio, you can select Azure Cloud Service in the New Project dialog box, under Python.

In the Azure Cloud Service wizard, you can create new web and worker roles.

The worker role template comes with boilerplate code to connect to an Azure storage account or Azure Service Bus.

You can add web or worker roles to an existing cloud service at any time. You can choose to add existing projects in your solution, or create new ones.

Your cloud service can contain roles implemented in different languages. For example, you can have a Python web role implemented using Django, with Python, or with C# worker roles. You can easily communicate between your roles using Service Bus queues or storage queues.

Install Python on the cloud service

Warning

The setup scripts that are installed with Visual Studio (at the time this article was last updated) do not work. This section describes a workaround.

The main problem with the setup scripts is that they do not install python. First, define two startup tasks in the ServiceDefinition.csdef file. The first task (PrepPython.ps1) downloads and installs the Python runtime. The second task (PipInstaller.ps1) runs pip to install any dependencies you may have.

The following scripts were written targeting Python 3.5. If you want to use the version 2.x of python, set the PYTHON2 variable file to on for the two startup tasks and the runtime task: <Variable name="PYTHON2" value="<mark>on</mark>" />.

ps.cmd

The Visual Studio templates should have created a ps.cmd file in the ./bin folder. This shell script calls out the PowerShell wrapper scripts above and provides logging based on the name of the PowerShell wrapper called. If this file wasn't created, here is what should be in it.

Run locally

If you set your cloud service project as the startup project and press F5, the cloud service runs in the local Azure emulator.

Although PTVS supports launching in the emulator, debugging (for example, breakpoints) does not work.

To debug your web and worker roles, you can set the role project as the startup project and debug that instead. You can also set multiple startup projects. Right-click the solution and then select Set StartUp Projects.

Publish to Azure

To publish, right-click the cloud service project in the solution and then select Publish.

Follow the wizard. If you need to, enable remote desktop. Remote desktop is helpful when you need to debug something.

When you are done configuring settings, click Publish.

Some progress appears in the output window, then you'll see the Microsoft Azure Activity Log window.

Deployment takes several minutes to complete, then your web and/or worker roles run on Azure!

Investigate logs

After the cloud service virtual machine starts up and installs Python, you can look at the logs to find any failure messages. These logs are located in the C:\Resources\Directory\{role}\LogFiles folder. PrepPython.err.txt has at least one error in it from when the script tries to detect if Python is installed and PipInstaller.err.txt may complain about an outdated version of pip.

Next steps

For more detailed information about working with web and worker roles in Python Tools for Visual Studio, see the PTVS documentation: