Forums

Long running tasks

I have been trying to implement the piece of code that is referenced here :

http://help.pythonanywhere.com/pages/LongRunningTasks

Not succeeding so far.

I'm a python newbie - can anyone tell me how /where to access the "log" information that I guess this code generates ? (from logging.debug, and logging.info ?).

=================================================

try:
    lock_id = "planetcooler.getCO2tonnesPVcombiV3"
    lock_socket.bind('\0' + lock_id)
    logging.debug("Acquired lock %r" % (lock_id,))
    return True
except socket.error:
    # socket already locked, task must be running
    logging.info("Failed to acquirelock %r" % (lock_id,))
    return False

============================================

Thanks for any pointers.......

How are you running the code? If it's from inside a scheduled task, you'll see a link to the log next to the task on the "Schedule" page.

I'm just "running' it !!

It's not a scheduled task.

Is this the way to go?

I just want it to restart automatically after a failure - for whatever reason.

You need to run it using a scheduled task. The way it works is that once an hour, when the task is kicked off, it checks to see if another copy is already running. If there is one, it just exits. If there isn't, it starts. Check out the help page again for more details.

Hi - thanks for this.

I am now using the Scheduler to launch a copy of getCO2tonnesPVcombiV3 every hour. I then go in once a day and manually kill the excess copies off. This is a sledgehammer approach! But works.

I can see how your example of Long Running tasks is meant to operate - but my code falls over. For some reason the "from xxxx import yyyyy" is failing to incorporate a method. I am probably using incorrect terminology / directory structure or something.............?

My version of your Longrunning task (viz Eternal.py):

!/usr/bin/python2.7

import logging import socket import sys

from getCO2tonnesPVcombiV3.py import getCO2tonnesPVcombiV3

lock_socket = None def is_lock_free(): global lock_socket lock_socket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) try: lock_id = "planetcooler.getCO2tonnesPVcombiV3" lock_socket.bind('\0' + lock_id) logging.debug("Acquired lock %r" % (lock_id,)) return True except socket.error: # socket already locked, task must be running logging.info("Failed to acquirelock %r" % (lock_id,)) return False if not is_lock_free(): sys.exit() getCO2tonnesPVcombiV3()

results in:

Traceback (most recent call last): File "/home/planetcooler/Eternal.py", line 6, in <module> from getCO2tonnesPVcombiV3.py import getCO2tonnesPVcombiV3 File "/home/planetcooler/getCO2tonnesPVcombiV3.py", line 63, in <module> response = querier.query(substation_id) NameError: name 'querier' is not defined

....any pointers gratefully received.........

Nearly there !

Regards and thanks .

oops that didn't format very well

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/python2.7
import logging
import socket
import sys
#
from getCO2tonnesPVcombiV3.py import getCO2tonnesPVcombiV3
#
lock_socket = None
def is_lock_free():
    global lock_socket
    lock_socket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
    try:
        lock_id = "planetcooler.getCO2tonnesPVcombiV3"
        lock_socket.bind('\0' + lock_id)
        logging.debug("Acquired lock %r" % (lock_id,))
        return True
    except socket.error:
        # socket already locked, task must be running
        logging.info("Failed to acquirelock %r" % (lock_id,))
        return False
if not is_lock_free():
    sys.exit()
getCO2tonnesPVcombiV3()

results in :

Traceback (most recent call last): File "/home/planetcooler/Eternal.py", line 6, in <module> from getCO2tonnesPVcombiV3.py import getCO2tonnesPVcombiV3 File "/home/planetcooler/getCO2tonnesPVcombiV3.py", line 63, in <module> response = querier.query(substation_id) NameError: name 'querier' is not defined

sounds like that's a problem in getCO2tonnesPVcombiV3.py rather than the long-running lock thing?

Thanks for that.

I might agree - except that I am running getCO2tonnesPVcombiV3.py standalone as a Scheduled Task ever hour.

Inserting it in this piece of code causes the failure to locate 'querier'. Think I must be getting the semantics of the "from xxxxxxxx import yyyyyy" wrong...............

Looking more closely at your code, this line looks wrong:

from getCO2tonnesPVcombiV3.py import getCO2tonnesPVcombiV3

That means "look along the system path until you find a directory called getCO2tonnesPVcombiV3, then look inside there for a file called py.py, then run that file and extract a variable/function that it defines called getCO2tonnesPVcombiV3"

What you probably mean is

from getCO2tonnesPVcombiV3 import getCO2tonnesPVcombiV3

...which means "look along the system path until you find a file called getCO2tonnesPVcombiV3.py, then run that file and extract a variable/function that it defines called getCO2tonnesPVcombiV3"

Thanks for your various efforts - much appreciated.

I am sorry - I am being extremely dense about this.

I have tried all variations of "from xxxxx import yyyyy" - to no avail.

Let's say I have a short piece of python in a file zzzz.py living in a directory called wwwwww

It runs perfectly well if I just "Run" it - until it eventually falls over or the console gets killed - whatever. Which is why I want to implement the code shown under Long Running tasks.

I think the one thing I definitely know is "my-username"

After that I am inventing stuff:

What do I need to call the "lock_id" ?

What is "my-task-name" ?

What is "my_module"

What is "my_long_running_process"

Sorry - probably elementary - but I'm missing something (a lot) re terminology.

Thanks for your patience !