How can I made a string that is updated by a thread reflect the changes on Python's curses?
I am planning to implement the curses library into an existing Python script for a client. The script will be run purely through SSH.
I am currently attempting to simulate some of the output that my script would generate.
In my 'testing-the-waters' script I have 3 variables: x, y, z.
I have a thread running alongside the curses loop that increments x, y, and z every x seconds. In the loop I am simply printing the three variables to the terminal screen.
The problem: The variables do not update until I provide some kind of input.
How can I make the terminal string update the values automagically?
I am testing this on a Terminal on Kubuntu. I tried Urwid and ran into a similar problem.
import curses
import time
from threading import Thread
x, y, z = 0, 0, 0
go = True
def increment_ints():
global x, y, z
while go:
x += 1
y += 2
z += 3
time.sleep(3)
def main(screen):
global go
curses.initscr()
screen.clear()
while go:
screen.addstr(0, 0, f"x: {x}, y = {y}, z = {z}")
c = screen.getch()
if c == ord('q'):
go = False
if __name__ == '__main__':
t = Thread(target=update_ints)
t.setDaemon(True)
t.start()
curses.wrapper(main)
Expected:
The values of x, y, and z are displayed and reflect the increments without input.
Actual results:
The values of x, y, and z remain 1, 2, and 3 respectively and updates only when I press a key.
-----------Edit:
This works as expected:
import curses
import time
from threading import Thread
x, y, z = 0, 0, 0
go = True
def update_ints():
global x, y, z
x += 1
y += 2
z += 3
def main(screen):
global go
curses.initscr()
screen.clear()
while go:
update_ints()
screen.addstr(0, 0, f"x: {x}, y = {y}, z = {z}")
c = screen.getch()
if c == ord('q'):
go = False
time.sleep(3)
if __name__ == '__main__':
curses.wrapper(main)
But I will need the values to be updated from a thread.
python terminal curses
|
show 1 more comment
I am planning to implement the curses library into an existing Python script for a client. The script will be run purely through SSH.
I am currently attempting to simulate some of the output that my script would generate.
In my 'testing-the-waters' script I have 3 variables: x, y, z.
I have a thread running alongside the curses loop that increments x, y, and z every x seconds. In the loop I am simply printing the three variables to the terminal screen.
The problem: The variables do not update until I provide some kind of input.
How can I make the terminal string update the values automagically?
I am testing this on a Terminal on Kubuntu. I tried Urwid and ran into a similar problem.
import curses
import time
from threading import Thread
x, y, z = 0, 0, 0
go = True
def increment_ints():
global x, y, z
while go:
x += 1
y += 2
z += 3
time.sleep(3)
def main(screen):
global go
curses.initscr()
screen.clear()
while go:
screen.addstr(0, 0, f"x: {x}, y = {y}, z = {z}")
c = screen.getch()
if c == ord('q'):
go = False
if __name__ == '__main__':
t = Thread(target=update_ints)
t.setDaemon(True)
t.start()
curses.wrapper(main)
Expected:
The values of x, y, and z are displayed and reflect the increments without input.
Actual results:
The values of x, y, and z remain 1, 2, and 3 respectively and updates only when I press a key.
-----------Edit:
This works as expected:
import curses
import time
from threading import Thread
x, y, z = 0, 0, 0
go = True
def update_ints():
global x, y, z
x += 1
y += 2
z += 3
def main(screen):
global go
curses.initscr()
screen.clear()
while go:
update_ints()
screen.addstr(0, 0, f"x: {x}, y = {y}, z = {z}")
c = screen.getch()
if c == ord('q'):
go = False
time.sleep(3)
if __name__ == '__main__':
curses.wrapper(main)
But I will need the values to be updated from a thread.
python terminal curses
1
Whydef increment_ints():
butt = Thread(target=update_ints)
?
– NEGR KITAEC
Jan 3 at 8:07
Thanks for the quick response! I am simulating my other script's infrastructure - it updates values from a thread and I want the displayed text to reflect the updated values.
– CoreCreatives
Jan 3 at 8:13
Does it work as expected if you replace curses calls with input and print?
– NEGR KITAEC
Jan 3 at 8:17
Yes. That would print eachx: {x}, y = {y}, z = {z}
line by line, but the desired input is one static screen with the values updated in place.
– CoreCreatives
Jan 3 at 8:20
1
Well you have to look into source code ofscreen.getch()
then
– NEGR KITAEC
Jan 3 at 8:26
|
show 1 more comment
I am planning to implement the curses library into an existing Python script for a client. The script will be run purely through SSH.
I am currently attempting to simulate some of the output that my script would generate.
In my 'testing-the-waters' script I have 3 variables: x, y, z.
I have a thread running alongside the curses loop that increments x, y, and z every x seconds. In the loop I am simply printing the three variables to the terminal screen.
The problem: The variables do not update until I provide some kind of input.
How can I make the terminal string update the values automagically?
I am testing this on a Terminal on Kubuntu. I tried Urwid and ran into a similar problem.
import curses
import time
from threading import Thread
x, y, z = 0, 0, 0
go = True
def increment_ints():
global x, y, z
while go:
x += 1
y += 2
z += 3
time.sleep(3)
def main(screen):
global go
curses.initscr()
screen.clear()
while go:
screen.addstr(0, 0, f"x: {x}, y = {y}, z = {z}")
c = screen.getch()
if c == ord('q'):
go = False
if __name__ == '__main__':
t = Thread(target=update_ints)
t.setDaemon(True)
t.start()
curses.wrapper(main)
Expected:
The values of x, y, and z are displayed and reflect the increments without input.
Actual results:
The values of x, y, and z remain 1, 2, and 3 respectively and updates only when I press a key.
-----------Edit:
This works as expected:
import curses
import time
from threading import Thread
x, y, z = 0, 0, 0
go = True
def update_ints():
global x, y, z
x += 1
y += 2
z += 3
def main(screen):
global go
curses.initscr()
screen.clear()
while go:
update_ints()
screen.addstr(0, 0, f"x: {x}, y = {y}, z = {z}")
c = screen.getch()
if c == ord('q'):
go = False
time.sleep(3)
if __name__ == '__main__':
curses.wrapper(main)
But I will need the values to be updated from a thread.
python terminal curses
I am planning to implement the curses library into an existing Python script for a client. The script will be run purely through SSH.
I am currently attempting to simulate some of the output that my script would generate.
In my 'testing-the-waters' script I have 3 variables: x, y, z.
I have a thread running alongside the curses loop that increments x, y, and z every x seconds. In the loop I am simply printing the three variables to the terminal screen.
The problem: The variables do not update until I provide some kind of input.
How can I make the terminal string update the values automagically?
I am testing this on a Terminal on Kubuntu. I tried Urwid and ran into a similar problem.
import curses
import time
from threading import Thread
x, y, z = 0, 0, 0
go = True
def increment_ints():
global x, y, z
while go:
x += 1
y += 2
z += 3
time.sleep(3)
def main(screen):
global go
curses.initscr()
screen.clear()
while go:
screen.addstr(0, 0, f"x: {x}, y = {y}, z = {z}")
c = screen.getch()
if c == ord('q'):
go = False
if __name__ == '__main__':
t = Thread(target=update_ints)
t.setDaemon(True)
t.start()
curses.wrapper(main)
Expected:
The values of x, y, and z are displayed and reflect the increments without input.
Actual results:
The values of x, y, and z remain 1, 2, and 3 respectively and updates only when I press a key.
-----------Edit:
This works as expected:
import curses
import time
from threading import Thread
x, y, z = 0, 0, 0
go = True
def update_ints():
global x, y, z
x += 1
y += 2
z += 3
def main(screen):
global go
curses.initscr()
screen.clear()
while go:
update_ints()
screen.addstr(0, 0, f"x: {x}, y = {y}, z = {z}")
c = screen.getch()
if c == ord('q'):
go = False
time.sleep(3)
if __name__ == '__main__':
curses.wrapper(main)
But I will need the values to be updated from a thread.
python terminal curses
python terminal curses
edited Jan 3 at 8:27
CoreCreatives
asked Jan 3 at 8:00
CoreCreativesCoreCreatives
1245
1245
1
Whydef increment_ints():
butt = Thread(target=update_ints)
?
– NEGR KITAEC
Jan 3 at 8:07
Thanks for the quick response! I am simulating my other script's infrastructure - it updates values from a thread and I want the displayed text to reflect the updated values.
– CoreCreatives
Jan 3 at 8:13
Does it work as expected if you replace curses calls with input and print?
– NEGR KITAEC
Jan 3 at 8:17
Yes. That would print eachx: {x}, y = {y}, z = {z}
line by line, but the desired input is one static screen with the values updated in place.
– CoreCreatives
Jan 3 at 8:20
1
Well you have to look into source code ofscreen.getch()
then
– NEGR KITAEC
Jan 3 at 8:26
|
show 1 more comment
1
Whydef increment_ints():
butt = Thread(target=update_ints)
?
– NEGR KITAEC
Jan 3 at 8:07
Thanks for the quick response! I am simulating my other script's infrastructure - it updates values from a thread and I want the displayed text to reflect the updated values.
– CoreCreatives
Jan 3 at 8:13
Does it work as expected if you replace curses calls with input and print?
– NEGR KITAEC
Jan 3 at 8:17
Yes. That would print eachx: {x}, y = {y}, z = {z}
line by line, but the desired input is one static screen with the values updated in place.
– CoreCreatives
Jan 3 at 8:20
1
Well you have to look into source code ofscreen.getch()
then
– NEGR KITAEC
Jan 3 at 8:26
1
1
Why
def increment_ints():
but t = Thread(target=update_ints)
?– NEGR KITAEC
Jan 3 at 8:07
Why
def increment_ints():
but t = Thread(target=update_ints)
?– NEGR KITAEC
Jan 3 at 8:07
Thanks for the quick response! I am simulating my other script's infrastructure - it updates values from a thread and I want the displayed text to reflect the updated values.
– CoreCreatives
Jan 3 at 8:13
Thanks for the quick response! I am simulating my other script's infrastructure - it updates values from a thread and I want the displayed text to reflect the updated values.
– CoreCreatives
Jan 3 at 8:13
Does it work as expected if you replace curses calls with input and print?
– NEGR KITAEC
Jan 3 at 8:17
Does it work as expected if you replace curses calls with input and print?
– NEGR KITAEC
Jan 3 at 8:17
Yes. That would print each
x: {x}, y = {y}, z = {z}
line by line, but the desired input is one static screen with the values updated in place.– CoreCreatives
Jan 3 at 8:20
Yes. That would print each
x: {x}, y = {y}, z = {z}
line by line, but the desired input is one static screen with the values updated in place.– CoreCreatives
Jan 3 at 8:20
1
1
Well you have to look into source code of
screen.getch()
then– NEGR KITAEC
Jan 3 at 8:26
Well you have to look into source code of
screen.getch()
then– NEGR KITAEC
Jan 3 at 8:26
|
show 1 more comment
1 Answer
1
active
oldest
votes
The issue was that c = screen.getch()
was blocking the loop and preventing the values from being updated.
Removing...
c = screen.getch()
if c == ord('q'):
go = False
... produced the intended results.
Thank you NEGR KITAEC
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54018395%2fhow-can-i-made-a-string-that-is-updated-by-a-thread-reflect-the-changes-on-pytho%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The issue was that c = screen.getch()
was blocking the loop and preventing the values from being updated.
Removing...
c = screen.getch()
if c == ord('q'):
go = False
... produced the intended results.
Thank you NEGR KITAEC
add a comment |
The issue was that c = screen.getch()
was blocking the loop and preventing the values from being updated.
Removing...
c = screen.getch()
if c == ord('q'):
go = False
... produced the intended results.
Thank you NEGR KITAEC
add a comment |
The issue was that c = screen.getch()
was blocking the loop and preventing the values from being updated.
Removing...
c = screen.getch()
if c == ord('q'):
go = False
... produced the intended results.
Thank you NEGR KITAEC
The issue was that c = screen.getch()
was blocking the loop and preventing the values from being updated.
Removing...
c = screen.getch()
if c == ord('q'):
go = False
... produced the intended results.
Thank you NEGR KITAEC
answered Jan 3 at 8:39
CoreCreativesCoreCreatives
1245
1245
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54018395%2fhow-can-i-made-a-string-that-is-updated-by-a-thread-reflect-the-changes-on-pytho%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Why
def increment_ints():
butt = Thread(target=update_ints)
?– NEGR KITAEC
Jan 3 at 8:07
Thanks for the quick response! I am simulating my other script's infrastructure - it updates values from a thread and I want the displayed text to reflect the updated values.
– CoreCreatives
Jan 3 at 8:13
Does it work as expected if you replace curses calls with input and print?
– NEGR KITAEC
Jan 3 at 8:17
Yes. That would print each
x: {x}, y = {y}, z = {z}
line by line, but the desired input is one static screen with the values updated in place.– CoreCreatives
Jan 3 at 8:20
1
Well you have to look into source code of
screen.getch()
then– NEGR KITAEC
Jan 3 at 8:26