-
Notifications
You must be signed in to change notification settings - Fork 4
/
idhelper.py
executable file
·97 lines (65 loc) · 2.19 KB
/
idhelper.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""
Author: Justin Cappos
Start Date: 19 July 2008
Description:
Provides a unique ID when requested...
This really should use uniqueid.repy
"""
import threading # to get the current thread name and a lock
# this dictionary contains keys that are thread names and values that are
# integers. The value starts at 0 and is incremented every time we give
# out an ID. The ID is formed from those two parts (thread name and ID)
uniqueid_idlist = [0]
uniqueid_idlock = threading.Lock()
def getuniqueid():
"""
<Purpose>
Provides a unique identifier.
<Arguments>
None
<Exceptions>
None.
<Side Effects>
None.
<Returns>
The identifier (the string)
"""
uniqueid_idlock.acquire()
# I'm using a list because I need a global, but don't want to use the global
# keyword.
myid = uniqueid_idlist[0]
uniqueid_idlist[0] = uniqueid_idlist[0] + 1
uniqueid_idlock.release()
myname = threading.currentThread().getName()
return myname + ":"+str(myid)
# thread_name_prefix is the prefix that is pre-pended to the counter
# The counter is the first element of an array, and starts indexing from 1.
# The thread_name_lock is used to serialize access to the counter
thread_name_prefix = "Thread:"
thread_name_counter = [1]
thread_name_lock = threading.Lock()
# Gets a new thread name. This call is thread safe, and returns a new
# unique name on each call.
def get_new_thread_name(extra_prefix=""):
"""
<Purpose>
Returns a new and unique name that can be associated with a new thread.
This is used so that threads can be uniquely identified.
This call is thread safe, and guarentees unique-ness.
<Arguments>
extra_prefix:
This is an optional string that is pre-pended to the
string that would be otherwise returned.
<Returns>
A string name
"""
# Acquire the lock
thread_name_lock.acquire()
# Construct the thread name
thread_name = str(extra_prefix) + thread_name_prefix + str(thread_name_counter[0])
# Increment the counter
thread_name_counter[0] = thread_name_counter[0] + 1
# Release the lock
thread_name_lock.release()
# Return the new name
return thread_name