Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
controller
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
TFS
controller
Commits
3a6da2d3
Commit
3a6da2d3
authored
1 year ago
by
Lluis Gifre Renom
Browse files
Options
Downloads
Patches
Plain Diff
Service component:
- Experimental (under development) concurrent task executor
parent
bcdf5225
Branches
feat/cttc-service-concurrent-task-executor
No related tags found
1 merge request
!109
Draft: EXPERIMENTAL Concurrent Task Executor for the Service component
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/service/experimental/test_concurrent_task_executor.py
+90
-0
90 additions, 0 deletions
src/service/experimental/test_concurrent_task_executor.py
with
90 additions
and
0 deletions
src/service/experimental/test_concurrent_task_executor.py
0 → 100644
+
90
−
0
View file @
3a6da2d3
# Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import
concurrent.futures
,
graphlib
,
random
,
threading
,
time
from
typing
import
Any
,
Dict
,
Optional
class
_Task
:
def
__init__
(
self
,
name
:
str
)
->
None
:
self
.
_name
=
name
@property
def
name
(
self
):
return
self
.
_name
def
execute
(
self
)
->
None
:
delay
=
1
# random.uniform(1, 5)
print
(
time
.
time
(),
'
task
'
,
self
.
_name
,
'
waiting
'
,
delay
)
time
.
sleep
(
delay
)
class
ConcurrentTaskExecutor
:
def
__init__
(
self
,
max_workers
:
Optional
[
int
]
=
None
,
thread_name_prefix
:
str
=
''
)
->
None
:
self
.
_max_workers
=
max_workers
self
.
_thread_name_prefix
=
thread_name_prefix
self
.
_tasks
:
Dict
[
str
,
_Task
]
=
dict
()
self
.
_dag
=
graphlib
.
TopologicalSorter
()
self
.
_lock
=
threading
.
Lock
()
self
.
_changed
=
threading
.
Event
()
def
add_task
(
self
,
task
:
_Task
,
*
predecessors
:
str
)
->
None
:
self
.
_tasks
[
task
.
name
]
=
task
if
len
(
predecessors
)
>
0
:
self
.
add_predecessors
(
task
.
name
,
*
predecessors
)
def
add_predecessors
(
self
,
task_name
:
str
,
*
predecessors
:
str
)
->
None
:
self
.
_dag
.
add
(
task_name
,
*
predecessors
)
def
run_task
(
self
,
task
:
_Task
)
->
None
:
print
(
time
.
time
(),
'
task
'
,
task
.
name
,
'
started
'
)
task
.
execute
()
print
(
time
.
time
(),
'
task
'
,
task
.
name
,
'
completed
'
)
with
self
.
_lock
:
self
.
_dag
.
done
(
task
.
name
)
self
.
_changed
.
set
()
def
execute
(
self
)
->
None
:
self
.
_dag
.
prepare
()
self
.
_changed
.
set
()
tpe_settings
=
dict
(
max_workers
=
self
.
_max_workers
,
thread_name_prefix
=
self
.
_thread_name_prefix
)
with
concurrent
.
futures
.
ThreadPoolExecutor
(
**
tpe_settings
)
as
executor
:
while
self
.
_changed
.
wait
():
with
self
.
_lock
:
if
not
self
.
_dag
.
is_active
():
break
self
.
_changed
.
clear
()
with
self
.
_lock
:
tasks
=
self
.
_dag
.
get_ready
()
print
(
time
.
time
(),
'
triggering tasks
'
,
tasks
)
for
task
in
tasks
:
executor
.
submit
(
self
.
run_task
,
task
)
cte
=
ConcurrentTaskExecutor
()
cte
.
add_task
(
_Task
(
'
svc:pkt1:planned
'
),
'
svc:root:planned
'
)
cte
.
add_task
(
_Task
(
'
svc:pkt2:planned
'
),
'
svc:root:planned
'
)
cte
.
add_task
(
_Task
(
'
svc:opt1:planned
'
),
'
svc:pkt1:planned
'
)
cte
.
add_task
(
_Task
(
'
svc:opt2:planned
'
),
'
svc:pkt2:planned
'
)
cte
.
add_task
(
_Task
(
'
con:opt1:config
'
),
'
svc:opt1:planned
'
)
cte
.
add_task
(
_Task
(
'
con:opt2:config
'
),
'
svc:opt2:planned
'
)
cte
.
add_task
(
_Task
(
'
svc:opt1:active
'
),
'
svc:opt1:planned
'
,
'
con:opt1:config
'
)
cte
.
add_task
(
_Task
(
'
svc:opt2:active
'
),
'
svc:opt2:planned
'
,
'
con:opt2:config
'
)
cte
.
add_task
(
_Task
(
'
con:pkt1:config
'
),
'
svc:pkt1:planned
'
,
'
svc:opt1:active
'
)
cte
.
add_task
(
_Task
(
'
con:pkt2:config
'
),
'
svc:pkt2:planned
'
,
'
svc:opt2:active
'
)
cte
.
add_task
(
_Task
(
'
svc:pkt1:active
'
),
'
svc:pkt1:planned
'
,
'
con:pkt1:config
'
)
cte
.
add_task
(
_Task
(
'
svc:pkt2:active
'
),
'
svc:pkt2:planned
'
,
'
con:pkt2:config
'
)
cte
.
add_task
(
_Task
(
'
svc:root:active
'
),
'
svc:pkt1:active
'
,
'
svc:pkt2:active
'
)
print
(
time
.
time
(),
'
started
'
)
cte
.
execute
()
print
(
time
.
time
(),
'
completed
'
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment