File: vine_python_cancel.py

package info (click to toggle)
cctools 1%3A7.14.5-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 36,956 kB
  • sloc: ansic: 114,614; python: 29,532; cpp: 20,313; sh: 13,675; perl: 4,056; xml: 3,688; makefile: 1,436
file content (58 lines) | stat: -rwxr-xr-x 1,365 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#! /usr/bin/env python3

# Test of task cancellation in Python.
# Submit 10 tasks that sleep for 10 seconds.
# After 3 seconds, cancel all of them.
# Wait for task return and verify that status was cancelled.

import sys
import ndcctools.taskvine as vine

ntasks = 10
ntasks_cancelled = 0

port_file = None
try:
    port_file = sys.argv[1]
except IndexError:
    sys.stderr.write("Usage: {} PORTFILE\n".format(sys.argv[0]))
    raise

q = vine.Manager(port=0)

with open(port_file, "w") as f:
    print("Writing port {port} to file {file}".format(port=q.port, file=port_file))
    f.write(str(q.port))

print("Submitting {} tasks...".format(ntasks))
    
for i in range(0,ntasks):
    t = vine.Task("sleep 10")
    t.set_cores(1)
    q.submit(t)

print("Waiting for tasks to start...".format(ntasks))

t = q.wait(3)

print("Cancelling all tasks...")
q.cancel_all()

print("Collecting cancelled tasks...")
while not q.empty():
    t = q.wait(10)
    if t:
        print("Task {} completed with status {}".format(t.id,t.result))
        if t.result=="cancelled":
                ntasks_cancelled+=1

                
if ntasks_cancelled==ntasks:
    print("Success: {} of {} tasks cancelled\n".format(ntasks_cancelled,ntasks))
    sys.exit(0)
else:
    print("Failure: {} of {} tasks cancelled\n".format(ntasks_cancelled,ntasks))
    sys.exit(1)