S. Van den Wyngaert
S. Van den Wyngaert

Reputation: 691

Django - Script called from view with call_command connects to Pyro server script but freezes on model access

Case:

I've got a Django application running, and got an interface with a list of accounts and their state. In the interface I can f.e. choose to start an account by clicking on a button.

This calls a view named 'start_session' which will start a script via the call_command function from django.core.management.base, with the account's primary key as argument. This is a Pyro client script which will then send a command to a running Pyro server script to start the account (for now it's just changing the state from 'OFF' to 'ON', but eventually it could be that it has to run and do things for hours).

Issue:

When I try to get an instance of the Account model matching the given primary key and try to change the state, or even try to return the account state to the Pyro client, it freezes. Note that if I just return the account state it only freezes until I interrupt the Pyro server script with CTRL-C, and after that the Pyro client script prints out the account state. But changing the accounts state never worked.

Any help or suggestions are appreciated!

Code:

views.py

from django.shortcuts import redirect
from django.contrib.auth.decorators import login_required
from django.core.management import call_command

@login_required
def start_session(request, account_pk):
    call_command('pyro_client', account_pk)
    return redirect('/control_panel')

pyro_client.py

import Pyro.core
from django.core.management.base import BaseCommand, CommandError

class Command(BaseCommand):
    def add_arguments(self, parser):
        # Positional arguments
        parser.add_argument('account_pk', nargs='+', type=str)        

    def handle(self, *args, **options):
        for account_pk in options['account_pk']:
            pyro_server = Pyro.core.getProxyForURI("PYRO://192.168.1.7:7766/c0a8010723ac22123332ff93e89e5328")
            pyro_server.command(account_pk, 'start')

pyro_server.py

import Pyro.core
from flipper.models import Account as AccountModel
import traceback

class PyroServer(Pyro.core.ObjBase):
    def __init__(self):
        Pyro.core.ObjBase.__init__(self)

    def command(self, account_pk, command_value):
        try:                
            if command_value == 'start':
                self.start_account(account_pk)

        except Exception, e:
            return traceback.format_exc()

    def start_account(self, account_pk):
        account = AccountModel.objects.get(pk=account_pk)

        account.state = 'ON'
        account.save()          

Pyro.core.initServer()
daemon=Pyro.core.Daemon()
uri=daemon.connect(PyroServer(),"pyro_server")

print "The daemon runs on port:",daemon.port
print "The object's uri is:",uri

daemon.requestLoop()

Upvotes: 0

Views: 273

Answers (1)

Irmen de Jong
Irmen de Jong

Reputation: 2847

You're linking to the Pyro4 library's documentation, but your code is using the ancient and no longer supported Pyro library version 3! Many problems will go away, including hopefully your "freeze" issue, by using the current version of Pyro4 instead (4.45 at the time of writing).

Upvotes: 0

Related Questions