Creating generic workflow id for thread+fork (perhaps get_ident_unique())

all,

I had a generic internals question. When I run get_ident() on a thread, then do a os fork and run get_ident() again on the child process, I get the same thread id. I understand that when you run fork you copy the process data, but I would have thought that the child would have had its thread id tweaked as to be unique. Or have an option to get such a unique id.

As it is, I’m using sys.settrace to trace the flow of my programs and get_ident() to figure out which file to log it, and it really confuses the output to have the two indistinguishable lines of execution log to the same place - I’d like to split the logging output to separate workflows as determined by a global, unique id.

In any case, I’ve tried using os.getpid() to disambiguate but for whatever reason, using this inside of sys.settrace blows the whole workflow up. So I’m stuck - this looks like it is going to need internal changes - but I thought I’d throw it out here for comment.

note - to show how confusing this is, get_ident showing the same value on both a parent and child forked off using exec, here is a small snippet of the logs that are coming out here.

The legend of this log is - using sys.settrace:

a. log statement
b. stack trace depth
c. running file and line
d. nanosecond time
e. code line being executed

as you can see it bounces around form parent to child, with the stack trace bouncing along with it. with one parent/child it is doable, with more than one child, it becomes an unintelligible mess:

4149245 - 16 - /usr/lib/python3.11/site-packages/ansible/playbook/base.py:144: 1689047816794778957:                     name = name[1:]
4149260 - 21 - /usr/lib/python3.11/site-packages/ansible/executor/process/result.py:74: 1689047816794781114:             self._cur_worker += 1
4149246 - 16 - /usr/lib/python3.11/site-packages/ansible/playbook/base.py:145: 1689047816794798960:                 base_attributes[name] = value
4149261 - 21 - /usr/lib/python3.11/site-packages/ansible/executor/process/result.py:75: 1689047816794800580:             if self._cur_worker >= len(self._workers): 
4149247 - 16 - /usr/lib/python3.11/site-packages/ansible/playbook/base.py:141: 1689047816794817696:         for (name, value) in getmembers(self.__class__):
4149262 - 21 - /usr/lib/python3.11/site-packages/ansible/executor/process/result.py:76: 1689047816794819021:                 self._cur_worker = 0
4149248 - 16 - /usr/lib/python3.11/site-packages/ansible/playbook/base.py:142: 1689047816794834656:             if isinstance(value, Attribute):
4149263 - 21 - /usr/lib/python3.11/site-packages/ansible/executor/process/result.py:78: 1689047816794836420:             try:
4149249 - 16 - /opt/hedvig/lib/python3.11/site-packages/ansible/playbook/base.py:143: 1689047816794851105:                 if name.startswith('_'):
4149264 - 21 - /usr/python3.11/site-packages/ansible/executor/process/result.py:79: 1689047816794852594:                 if not rslt_q.empty():
4149250 - 16 - /usr/lib/python3.11/site-packages/ansible/playbook/base.py:144: 1689047816794868466:                     name = name[1:]