Skip to content
This repository has been archived by the owner on Feb 1, 2023. It is now read-only.

Commit

Permalink
Changing creation of unique tagnames uses uuids now to allow multiple…
Browse files Browse the repository at this point in the history
… executions at the same time without tagname collision
  • Loading branch information
miile7 committed Mar 18, 2021
1 parent 6a8f6fc commit 2577426
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
Version 1.1.9
=============

- Changing creation of unique persistent tag names from timestamp with milliseconds to using uuids
to fix tag collision in parallel threads or immediately consecutive calls

Version 1.1.8 (Hotfix)
======================

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.8
1.1.9
4 changes: 1 addition & 3 deletions execdmscript/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@
```
This uses the persistent tags to communicate from dm-script to python.
Note that this is not thread-safe!
"""

from .execdmscript import *

__version__ = "1.1.8"
__version__ = "1.1.9"
34 changes: 25 additions & 9 deletions execdmscript/execdmscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import re
import sys
import time
import uuid
import errno
import types
import random
Expand Down Expand Up @@ -636,6 +637,21 @@ def get_persistent_tag(path: typing.Optional[typing.Union[typing.Sequence[str],
raise KeyError(("No value was found in the persistent tags for " +
"the path '{}'.").format(path))

def unique_tag(tagname: str) -> str:
"""Get the `tagname` with an appended UUID.
Parameters
----------
tagname : str
The tagname
Returns
-------
str
A unique tagname to use
"""
return "{}_{}".format(tagname, str(uuid.uuid4()).replace("-", "_"))

class DMScriptWrapper:
"""Wraps one or more dm-scripts.
"""
Expand Down Expand Up @@ -689,8 +705,8 @@ def __init__(self,
will be placed in the current working directory, default: None
"""
self.scripts = DMScriptWrapper.normalizeScripts(scripts)
self._creation_time_id = str(round(time.time() * 100))
self.persistent_tag = "python-dm-communication-" + self._creation_time_id
self._unique_id = str(uuid.uuid4()).replace("-", "_")
self.persistent_tag = "__execdmscript_{}".format(self._unique_id)
self.readvars = readvars
self.setvars = setvars
self.synchronized_vars = {}
Expand Down Expand Up @@ -1060,9 +1076,9 @@ def getSeparateThreadStartCode(self, index: int) -> str:
"""

return "\n".join((
"object thread_cancel_signal{}_{} = NewCancelSignal();".format(self._creation_time_id, index),
"object thread_done_signal{}_{} = NewSignal(0);".format(self._creation_time_id, index),
"class ExecDMScriptThread{}_{} : Thread{{".format(self._creation_time_id, index),
"object thread_cancel_signal{}_{} = NewCancelSignal();".format(self._unique_id, index),
"object thread_done_signal{}_{} = NewSignal(0);".format(self._unique_id, index),
"class ExecDMScriptThread{}_{} : Thread{{".format(self._unique_id, index),
"void RunThread(object self){"
))

Expand All @@ -1088,11 +1104,11 @@ def getSeparateThreadEndCode(self, index: int) -> str:

return "\n".join((
"// inform that the thread is done now",
"thread_done_signal{}_{}.setSignal();".format(self._creation_time_id, index),
"thread_done_signal{}_{}.setSignal();".format(self._unique_id, index),
"}", # end ExecDMScriptThread<id>::RunThread()
"}", # end ExecDMScriptThread<id> class
"alloc(ExecDMScriptThread{}_{}).StartThread();".format(
self._creation_time_id, index
self._unique_id, index
)
))

Expand All @@ -1114,7 +1130,7 @@ def getSeparateThreadWaitCode(self, index: int) -> str:
return "\n".join((
"// wait for the thread {}".format(index),
"thread_done_signal{id}_{i}.WaitOnSignal(infinity(), thread_cancel_signal{id}_{i});".format(
id=self._creation_time_id, i=index
id=self._unique_id, i=index
)
))

Expand Down Expand Up @@ -1153,7 +1169,7 @@ def getSyncDMCode(self) -> str:
dmscript = []

# the name of the tag group to use
sync_code_tg_name = "sync_taggroup_" + self._creation_time_id
sync_code_tg_name = "sync_taggroup_" + self._unique_id

# declare and initialize the used variables
sync_code_prefix = "\n".join((
Expand Down

0 comments on commit 2577426

Please sign in to comment.