File: child.py

package info (click to toggle)
python-sysv-ipc 1.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 540 kB
  • sloc: ansic: 3,140; python: 1,960; makefile: 8; sh: 4
file content (23 lines) | stat: -rw-r--r-- 604 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
import sysv_ipc
import time
import sys
import random

# The parent passes the semaphore's name to me.
key = sys.argv[1]

sem = sysv_ipc.Semaphore(int(key))

print('Child: waiting to aquire semaphore ' + key)

with sem:
    print('Child: semaphore {} aquired; holding for 3 seconds.'.format(sem.key))

    # Flip a coin to determine whether or not to bail out of the context.
    if random.randint(0, 1):
        print("Child: raising ValueError to demonstrate unplanned context exit")
        raise ValueError

    time.sleep(3)

    print('Child: gracefully exiting context (releasing the semaphore).')