why couldnt achieve shared resource in python for twice(thread,timer
this whole script depreciate the NMEA code that i take from serial port. and gave this error(actually give neither error nor desired :
A: b'NX,1,1,2POTR1.01n'
B: b'NX0110,WVR=P 0*6r$GTT000,RVE=80*1r$NTT1002GSGLGALBS7n'
B: b'NX,1,2,BS;IS;S4n'
A: b'GX,0,1,,P;O;;D*7r$GTT0,010SAMEQZS*9r$GTT0002GSST=PSO*7rn'
A: b'X,0012LL=FFFFFFFF-FFF-FFF-FFFFx00*2n'
B: b'NX,1,1,,N OPG;GL3$GNTT1,,0,CFFFF-FFFFFFFFFFFFFFFFFFr$GX,1,,0,ANSPV= DS SEn'
B: b'GT,0,10,NSTTU=O*25n'
B: b'$NX,012,F3F4Bn'
The code:
from threading import Thread
import serial
import time
def runA():
while True:
for line in ser:
print("A: ",line)
def runB():
while True:
for line in ser:
print("B: ",line)
print("hey")
if __name__ == "__main__":
ser=serial.Serial('COM9',9600)
t1 = Thread(target = runA)
t2 = Thread(target = runB)
t1.setDaemon(True)
t2.setDaemon(True)
t1.start()
t2.start()
while True:
pass
expected is:
b'$GNGNS,080647.00,4059.11284,N,02907.92374,E,AA,13,0.84,130.5,37.5,,*6Crn'
b'$GNGGA,080647.00,4059.11284,N,02907.92374,E,1,12,0.84,130.5,M,37.5,M,,*47rn'
b'$GNGSA,A,3,09,22,01,11,17,18,23,03,31,,,,1.51,0.84,1.25*14rn'
b'$GNGSA,A,3,81,76,82,67,,,,,,,,,1.51,0.84,1.25*10rn'
b'$GPGSV,4,1,13,01,57,177,31,03,72,351,24,06,05,322,,09,24,241,30*72rn'
b'$GPGSV,4,2,13,11,28,189,17,14,14,052,,17,23,281,19,18,28,161,22*77rn'
I couldnt get data from same resource. Normally i know how to run two loops simultaneously but why didn't it work properly for this?
Thanks so much.
python multithreading timer wxpython
add a comment |
this whole script depreciate the NMEA code that i take from serial port. and gave this error(actually give neither error nor desired :
A: b'NX,1,1,2POTR1.01n'
B: b'NX0110,WVR=P 0*6r$GTT000,RVE=80*1r$NTT1002GSGLGALBS7n'
B: b'NX,1,2,BS;IS;S4n'
A: b'GX,0,1,,P;O;;D*7r$GTT0,010SAMEQZS*9r$GTT0002GSST=PSO*7rn'
A: b'X,0012LL=FFFFFFFF-FFF-FFF-FFFFx00*2n'
B: b'NX,1,1,,N OPG;GL3$GNTT1,,0,CFFFF-FFFFFFFFFFFFFFFFFFr$GX,1,,0,ANSPV= DS SEn'
B: b'GT,0,10,NSTTU=O*25n'
B: b'$NX,012,F3F4Bn'
The code:
from threading import Thread
import serial
import time
def runA():
while True:
for line in ser:
print("A: ",line)
def runB():
while True:
for line in ser:
print("B: ",line)
print("hey")
if __name__ == "__main__":
ser=serial.Serial('COM9',9600)
t1 = Thread(target = runA)
t2 = Thread(target = runB)
t1.setDaemon(True)
t2.setDaemon(True)
t1.start()
t2.start()
while True:
pass
expected is:
b'$GNGNS,080647.00,4059.11284,N,02907.92374,E,AA,13,0.84,130.5,37.5,,*6Crn'
b'$GNGGA,080647.00,4059.11284,N,02907.92374,E,1,12,0.84,130.5,M,37.5,M,,*47rn'
b'$GNGSA,A,3,09,22,01,11,17,18,23,03,31,,,,1.51,0.84,1.25*14rn'
b'$GNGSA,A,3,81,76,82,67,,,,,,,,,1.51,0.84,1.25*10rn'
b'$GPGSV,4,1,13,01,57,177,31,03,72,351,24,06,05,322,,09,24,241,30*72rn'
b'$GPGSV,4,2,13,11,28,189,17,14,14,052,,17,23,281,19,18,28,161,22*77rn'
I couldnt get data from same resource. Normally i know how to run two loops simultaneously but why didn't it work properly for this?
Thanks so much.
python multithreading timer wxpython
add a comment |
this whole script depreciate the NMEA code that i take from serial port. and gave this error(actually give neither error nor desired :
A: b'NX,1,1,2POTR1.01n'
B: b'NX0110,WVR=P 0*6r$GTT000,RVE=80*1r$NTT1002GSGLGALBS7n'
B: b'NX,1,2,BS;IS;S4n'
A: b'GX,0,1,,P;O;;D*7r$GTT0,010SAMEQZS*9r$GTT0002GSST=PSO*7rn'
A: b'X,0012LL=FFFFFFFF-FFF-FFF-FFFFx00*2n'
B: b'NX,1,1,,N OPG;GL3$GNTT1,,0,CFFFF-FFFFFFFFFFFFFFFFFFr$GX,1,,0,ANSPV= DS SEn'
B: b'GT,0,10,NSTTU=O*25n'
B: b'$NX,012,F3F4Bn'
The code:
from threading import Thread
import serial
import time
def runA():
while True:
for line in ser:
print("A: ",line)
def runB():
while True:
for line in ser:
print("B: ",line)
print("hey")
if __name__ == "__main__":
ser=serial.Serial('COM9',9600)
t1 = Thread(target = runA)
t2 = Thread(target = runB)
t1.setDaemon(True)
t2.setDaemon(True)
t1.start()
t2.start()
while True:
pass
expected is:
b'$GNGNS,080647.00,4059.11284,N,02907.92374,E,AA,13,0.84,130.5,37.5,,*6Crn'
b'$GNGGA,080647.00,4059.11284,N,02907.92374,E,1,12,0.84,130.5,M,37.5,M,,*47rn'
b'$GNGSA,A,3,09,22,01,11,17,18,23,03,31,,,,1.51,0.84,1.25*14rn'
b'$GNGSA,A,3,81,76,82,67,,,,,,,,,1.51,0.84,1.25*10rn'
b'$GPGSV,4,1,13,01,57,177,31,03,72,351,24,06,05,322,,09,24,241,30*72rn'
b'$GPGSV,4,2,13,11,28,189,17,14,14,052,,17,23,281,19,18,28,161,22*77rn'
I couldnt get data from same resource. Normally i know how to run two loops simultaneously but why didn't it work properly for this?
Thanks so much.
python multithreading timer wxpython
this whole script depreciate the NMEA code that i take from serial port. and gave this error(actually give neither error nor desired :
A: b'NX,1,1,2POTR1.01n'
B: b'NX0110,WVR=P 0*6r$GTT000,RVE=80*1r$NTT1002GSGLGALBS7n'
B: b'NX,1,2,BS;IS;S4n'
A: b'GX,0,1,,P;O;;D*7r$GTT0,010SAMEQZS*9r$GTT0002GSST=PSO*7rn'
A: b'X,0012LL=FFFFFFFF-FFF-FFF-FFFFx00*2n'
B: b'NX,1,1,,N OPG;GL3$GNTT1,,0,CFFFF-FFFFFFFFFFFFFFFFFFr$GX,1,,0,ANSPV= DS SEn'
B: b'GT,0,10,NSTTU=O*25n'
B: b'$NX,012,F3F4Bn'
The code:
from threading import Thread
import serial
import time
def runA():
while True:
for line in ser:
print("A: ",line)
def runB():
while True:
for line in ser:
print("B: ",line)
print("hey")
if __name__ == "__main__":
ser=serial.Serial('COM9',9600)
t1 = Thread(target = runA)
t2 = Thread(target = runB)
t1.setDaemon(True)
t2.setDaemon(True)
t1.start()
t2.start()
while True:
pass
expected is:
b'$GNGNS,080647.00,4059.11284,N,02907.92374,E,AA,13,0.84,130.5,37.5,,*6Crn'
b'$GNGGA,080647.00,4059.11284,N,02907.92374,E,1,12,0.84,130.5,M,37.5,M,,*47rn'
b'$GNGSA,A,3,09,22,01,11,17,18,23,03,31,,,,1.51,0.84,1.25*14rn'
b'$GNGSA,A,3,81,76,82,67,,,,,,,,,1.51,0.84,1.25*10rn'
b'$GPGSV,4,1,13,01,57,177,31,03,72,351,24,06,05,322,,09,24,241,30*72rn'
b'$GPGSV,4,2,13,11,28,189,17,14,14,052,,17,23,281,19,18,28,161,22*77rn'
I couldnt get data from same resource. Normally i know how to run two loops simultaneously but why didn't it work properly for this?
Thanks so much.
python multithreading timer wxpython
python multithreading timer wxpython
asked Dec 28 '18 at 5:27
gcsgcs
12
12
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
there are a bunch of problems with this ... its not starved for resources ...
probably the main one is though that
for line in serialPort is not part of the serial interface
I think you mean to do
for line in iter(ser.readline,''): which will blockingly wait for a newline
but i expect you will get garbled output trying to consume from the same port in both threads ... but it wont block
this assumes that you can even access ser inside of your functions ... since its scope is the if __name__ == "__main__" and i think python3 fixes some leaky scope issues
I actually am pretty sure both your threads are just crashing out... and your main loop is just spinning
def runA(): ser=serial.Serial('COM9',9600) while True: for line in iter(ser.readline,''): print("A: ",line) runA() this works. ı dont have any problem with serial, my problem is using shared resource with thread. Thanks for your effort btw
– gcs
Dec 28 '18 at 6:06
your code is doingfor line in ser... not what I put, or what you put ... you need to dofor line in iter(ser.readline,''):...
– Joran Beasley
Dec 28 '18 at 6:10
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%2f53954037%2fwhy-couldnt-achieve-shared-resource-in-python-for-twicethread-timer%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
there are a bunch of problems with this ... its not starved for resources ...
probably the main one is though that
for line in serialPort is not part of the serial interface
I think you mean to do
for line in iter(ser.readline,''): which will blockingly wait for a newline
but i expect you will get garbled output trying to consume from the same port in both threads ... but it wont block
this assumes that you can even access ser inside of your functions ... since its scope is the if __name__ == "__main__" and i think python3 fixes some leaky scope issues
I actually am pretty sure both your threads are just crashing out... and your main loop is just spinning
def runA(): ser=serial.Serial('COM9',9600) while True: for line in iter(ser.readline,''): print("A: ",line) runA() this works. ı dont have any problem with serial, my problem is using shared resource with thread. Thanks for your effort btw
– gcs
Dec 28 '18 at 6:06
your code is doingfor line in ser... not what I put, or what you put ... you need to dofor line in iter(ser.readline,''):...
– Joran Beasley
Dec 28 '18 at 6:10
add a comment |
there are a bunch of problems with this ... its not starved for resources ...
probably the main one is though that
for line in serialPort is not part of the serial interface
I think you mean to do
for line in iter(ser.readline,''): which will blockingly wait for a newline
but i expect you will get garbled output trying to consume from the same port in both threads ... but it wont block
this assumes that you can even access ser inside of your functions ... since its scope is the if __name__ == "__main__" and i think python3 fixes some leaky scope issues
I actually am pretty sure both your threads are just crashing out... and your main loop is just spinning
def runA(): ser=serial.Serial('COM9',9600) while True: for line in iter(ser.readline,''): print("A: ",line) runA() this works. ı dont have any problem with serial, my problem is using shared resource with thread. Thanks for your effort btw
– gcs
Dec 28 '18 at 6:06
your code is doingfor line in ser... not what I put, or what you put ... you need to dofor line in iter(ser.readline,''):...
– Joran Beasley
Dec 28 '18 at 6:10
add a comment |
there are a bunch of problems with this ... its not starved for resources ...
probably the main one is though that
for line in serialPort is not part of the serial interface
I think you mean to do
for line in iter(ser.readline,''): which will blockingly wait for a newline
but i expect you will get garbled output trying to consume from the same port in both threads ... but it wont block
this assumes that you can even access ser inside of your functions ... since its scope is the if __name__ == "__main__" and i think python3 fixes some leaky scope issues
I actually am pretty sure both your threads are just crashing out... and your main loop is just spinning
there are a bunch of problems with this ... its not starved for resources ...
probably the main one is though that
for line in serialPort is not part of the serial interface
I think you mean to do
for line in iter(ser.readline,''): which will blockingly wait for a newline
but i expect you will get garbled output trying to consume from the same port in both threads ... but it wont block
this assumes that you can even access ser inside of your functions ... since its scope is the if __name__ == "__main__" and i think python3 fixes some leaky scope issues
I actually am pretty sure both your threads are just crashing out... and your main loop is just spinning
answered Dec 28 '18 at 5:35
Joran BeasleyJoran Beasley
72.4k677117
72.4k677117
def runA(): ser=serial.Serial('COM9',9600) while True: for line in iter(ser.readline,''): print("A: ",line) runA() this works. ı dont have any problem with serial, my problem is using shared resource with thread. Thanks for your effort btw
– gcs
Dec 28 '18 at 6:06
your code is doingfor line in ser... not what I put, or what you put ... you need to dofor line in iter(ser.readline,''):...
– Joran Beasley
Dec 28 '18 at 6:10
add a comment |
def runA(): ser=serial.Serial('COM9',9600) while True: for line in iter(ser.readline,''): print("A: ",line) runA() this works. ı dont have any problem with serial, my problem is using shared resource with thread. Thanks for your effort btw
– gcs
Dec 28 '18 at 6:06
your code is doingfor line in ser... not what I put, or what you put ... you need to dofor line in iter(ser.readline,''):...
– Joran Beasley
Dec 28 '18 at 6:10
def runA(): ser=serial.Serial('COM9',9600) while True: for line in iter(ser.readline,''): print("A: ",line) runA() this works. ı dont have any problem with serial, my problem is using shared resource with thread. Thanks for your effort btw
– gcs
Dec 28 '18 at 6:06
def runA(): ser=serial.Serial('COM9',9600) while True: for line in iter(ser.readline,''): print("A: ",line) runA() this works. ı dont have any problem with serial, my problem is using shared resource with thread. Thanks for your effort btw
– gcs
Dec 28 '18 at 6:06
your code is doing
for line in ser ... not what I put, or what you put ... you need to do for line in iter(ser.readline,''): ...– Joran Beasley
Dec 28 '18 at 6:10
your code is doing
for line in ser ... not what I put, or what you put ... you need to do for line in iter(ser.readline,''): ...– Joran Beasley
Dec 28 '18 at 6:10
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53954037%2fwhy-couldnt-achieve-shared-resource-in-python-for-twicethread-timer%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