Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
adc2018-system
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
adc2018
adc2018-system
Commits
b68f8105
Commit
b68f8105
authored
Aug 10, 2018
by
Kento HASEGAWA
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make it possible to stop a running solver (#7)
parent
b33ac986
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
19 additions
and
60 deletions
+19
-60
main.py
comm/client/main.py
+19
-60
No files found.
comm/client/main.py
View file @
b68f8105
...
...
@@ -17,65 +17,17 @@ from urllib.parse import urlparse
sys
.
path
.
append
(
os
.
path
.
dirname
(
os
.
path
.
abspath
(
__file__
))
+
'/../../solver'
)
import
BoardStr
import
pynqrouter
import
adc2018solver
as
pynqrouter
app
=
Flask
(
__name__
)
args
=
{}
pynq_thread
=
None
#
pynq_thread = None
client_baseurl
=
""
# Reference: https://teratail.com/questions/52593
class
StoppableThread
(
threading
.
Thread
):
def
__init__
(
self
,
target
,
qname
,
qstr
,
qseed
):
super
(
StoppableThread
,
self
)
.
__init__
(
target
=
target
)
self
.
_th_stop
=
threading
.
Event
()
self
.
_qname
=
qname
self
.
_qstr
=
qstr
self
.
_qseed
=
int
(
qseed
)
self
.
_answer
=
None
def
run
(
self
):
global
client_baseurl
global
pynq_thread
global
args
# Main funciton (the solver should be placed here)
boardstr
=
BoardStr
.
conv_boardstr
(
self
.
_qstr
.
split
(
'
\n
'
),
'random'
,
self
.
_qseed
)
result
=
pynqrouter
.
solve
(
boardstr
,
self
.
_qseed
)
if
result
[
'solved'
]:
answer
=
result
[
'solution'
]
else
:
answer
=
None
pynq_thread
=
None
return
res
=
{
"client"
:
client_baseurl
,
"qname"
:
self
.
_qname
,
"answer"
:
answer
,
"cputime"
:
result
[
'elapsed'
]
}
self
.
_answer
=
answer
r
=
requests
.
post
(
"http://{}/post"
.
format
(
args
[
"host"
]),
data
=
res
)
if
args
[
"verbose"
]:
print
(
res
)
pynq_thread
=
None
def
stop
(
self
):
self
.
_th_stop
.
set
()
def
stopped
(
self
):
return
self
.
_th_stop
.
isSet
()
def
get_answer
(
self
):
return
self
.
_answer
@
app
.
route
(
'/start'
,
methods
=
[
"POST"
])
def
start
():
global
pynq_thread
#
global pynq_thread
global
args
global
client_baseurl
...
...
@@ -84,13 +36,19 @@ def start():
if
args
[
"verbose"
]:
print
(
request
.
form
)
if
pynq_thread
is
None
:
if
pynqrouter
.
solver_thread
is
not
None
:
pynqrouter
.
stop_solver
()
if
pynqrouter
.
solver_thread
is
None
:
qstr
=
request
.
form
[
"question"
]
qname
=
request
.
form
[
"qname"
]
qseed
=
request
.
form
[
"qseed"
]
pynq_thread
=
StoppableThread
(
target
=
StoppableThread
,
qname
=
qname
,
qstr
=
qstr
,
qseed
=
qseed
)
boardstr
=
BoardStr
.
conv_boardstr
(
qstr
.
split
(
'
\n
'
),
'random'
,
int
(
qseed
))
option
=
{
"name"
:
qname
,
"host"
:
args
[
'host'
],
"client"
:
client_baseurl
}
pynq_thread
.
start
()
pynqrouter
.
start_solver
(
boardstr
,
qseed
,
option
)
# pynq_thread = pynqrouter.start_solver(boardstr, qseed, option)
# 実行だけ開始する
ans
[
"status"
]
=
"Processing"
...
...
@@ -106,26 +64,27 @@ def start():
@
app
.
route
(
'/stop'
)
def
stop
():
global
pynq_thread
#
global pynq_thread
if
pynq_thread
is
None
:
if
pynq
router
.
solver
_thread
is
None
:
ans
=
{
"status"
:
"No threads"
}
else
:
pynq_thread
.
stop
()
# pynq_thread.stop()
pynqrouter
.
stop_solver
()
ans
=
{
"status"
:
"Stopped"
}
pynq_thread
=
None
#
pynq_thread = None
return
json
.
dumps
(
ans
)
@
app
.
route
(
"/status"
)
def
status
():
global
pynq_thread
#
global pynq_thread
res_mes
=
""
if
pynq_thread
is
None
:
if
pynq
router
.
solver
_thread
is
None
:
res_mes
=
"Ready"
else
:
res_mes
=
"Working"
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment