Skip to content

Instantly share code, notes, and snippets.

@r614
Created September 7, 2024 00:46
Show Gist options
  • Save r614/5d2f4b5e00f05ddecf9541a9acbaa8c6 to your computer and use it in GitHub Desktop.
Save r614/5d2f4b5e00f05ddecf9541a9acbaa8c6 to your computer and use it in GitHub Desktop.
modal job sync

Modal Job Sync

Launching a job

import modal

remote_fn = modal.Function.lookup(<namespace>, <fn_name>)
fn_call = remote_fn.spawn(job_id)
unique_job_id = fn_call.object_id # store this in db 

Sync job state

import modal

pending_job_ids = [] # fetch from db 

for call_id in pending_job_ids:

   function_call = modal.functions.FunctionCall.from_id(call_id)
   
   try:
       result = function_call.get(timeout=0)
       print(f"job {call_id} completed with result {result}")
   except TimeoutError:
       print(f"job {call_id} still pending")
    except Exception as e:
       print(f"job {call_id} failed with exception {e}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment