Re: [Tutor] threading in python 2.7 - 2nd version
Hi, My answers are below. -Original Message- From: Tutor [mailto:tutor-bounces+joseph.lee22590=gmail@python.org] On Behalf Of Rance Hall Sent: Sunday, January 4, 2015 9:20 PM To: tutor Subject: [Tutor] threading in python 2.7 - 2nd version Thanks to the advice from Joseph and Alan, I hacked a quick python script which demonstrates my problem more accurately. Its not board specific as was my last code. This sample works the same on my pcduino as it does on my desktop. #threading problem example import threading import sys import time threads = [] exitFlag = 0 def delay(ms): time.sleep(1.0*ms/1000) def threadloop(): while not exitFlag: print "exitFlag value: ", exitFlag delay(2000) def cleanup(): exitFlag = 1 print "Exit flag value: ", exitFlag for t in threads: t.join() sys.exit() JL: Hmmm, can you take a look at the above code for cleanup function? If you look at it carefully, you'll see why the program will enter infinite loop. Specifically, take a look at the following textual chart: Function : exitFlag : threads main : global.exitFlag : global.threads loop : global.exitFlag : global.threads cleanup : cleanup.exitFlag : cleanup.threads As you can see, something odd is going on in cleanup function: it has its own exit flag and threads pool. If you ever need to peek a look at a variable outside of a given function (local scope) nad mess with it, you need to add the following at the beginning of the function in question: Gglobal *globalVars # I used * in there to denote unknown number of variables. In other words, the bug had to do with scope resolution, which is very important when threads need to access or modify global variables. That is, just because you give the variable the same name as a global flag in a function doesn't mean you are reading the actual global flags, which is what the threads here were trying to do. Original: the thread driven loop doesn't ever see the fact that the exitFlag as changed, based on the output to screen. Be warned, this code gives you an infinite loop, so be sure to run it in a terminal you can kill without impacting other work you are doing. There are many ways to work with theads. Class definitions, etc. The thread and threading modules. I've tried several and get the same results. What do I need to do to get the thread to stop based on the value of exitFlag? jL: See my comment and flow chart above. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Could you look over my code?
On Sun, Jan 04, 2015 at 09:53:37PM -0600, boB Stepp wrote: > On Sun, Jan 4, 2015 at 8:39 PM, Steven D'Aprano wrote: [...] > > Don't be distressed! You've just stumbled across a small difference > > between Python version 2 and version 3. In Python 3, you should use > > "raw_input" instead of "input", so your code will look like this: > > In the paragraph above, I believe that Steven meant to say, "... > version 3. In Python 2, you should use ...", which should be evident > from his earlier comments. D'oh! I mean, well done Bob, you have passed my cunning test to see who was paying attention. :-) Yes, you are correct. Use raw_input in Python 2, and input in Python 3. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] threading in python 2.7 - 2nd version
Cameron: This was the exact issue, and I had initially suspected as much, but had no idea how to fix it. I had tried to use the global variable directive, but as it turned out I had misused it. Anyway, everything is working as it should. Thanks Rance On Mon, Jan 5, 2015 at 12:39 AM, Cameron Simpson wrote: > On 04Jan2015 23:19, Rance Hall wrote: > >> Thanks to the advice from Joseph and Alan, I hacked a quick python script >> which demonstrates my problem more accurately. >> Its not board specific as was my last code. This sample works the same on >> my pcduino as it does on my desktop. [...] >> >> [...] >> exitFlag = 0 >> > > Ok. > > def threadloop(): >>while not exitFlag: >>print "exitFlag value: ", exitFlag >>delay(2000) >> >> def cleanup(): >>exitFlag = 1 >>print "Exit flag value: ", exitFlag >>for t in threads: >>t.join() >>sys.exit() >> > > These two hold your issue. > > threadloop() _does_ consult the global "exitFlag". But only because you > never assign to it in this function. So when you consult the name > "exitFlag", it looks for a local variable and does not find one, so it > looks in the global scope and finds it there. > > cleanup() uses exitFlag as a _local_ variable (like most variables in > python). This is because it assigns to it. Python will always use a local > variable for something you assign to unless you tell it not to; that (by > default) keeps the effects of a function within the function. Because of > this, cleanup does not affect the global variable. > > Here, it would be best to add the line: > >global exitFlag > > at the top of _both_ functions, to be clear that you are accessing the > global in both cases. So: > > def threadloop(): > global exitFlag > while not exitFlag: > print "exitFlag value: ", exitFlag > delay(2000) > def cleanup(): > global exitFlag > exitFlag = 1 > print "Exit flag value: ", exitFlag > for t in threads: > t.join() > sys.exit() > > Cheers, > Cameron Simpson > > Out of memory. > We wish to hold the whole sky, > But we never will. > - Haiku Error Messages http://www.salonmagazine.com/ > 21st/chal/1998/02/10chal2.html > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fwd: Newbie
On 02/01/2015 18:21, Rohan Ds wrote: -- Forwarded message -- From: "Rohan Ds" Date: 2 Jan 2015 23:46 Subject: Newbie To: Cc: Hello everybody :) I am Rohan from India. I'm new to Python. I have a basic understanding as to how Python works. I want to contribute to PSF. The information available on the site didn't really answer my questions. If anyone here could tell me, how I should get started with it, I'd be really grateful to you. Also, with GSoC coming up, if i keep contributing dedicatedly to PSF, will I have a shot at GSoC? Someone please reply asap. https://mail.python.org/mailman/listinfo/core-mentorship -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Improving My Simple Game Code for Speed, Memory and Learning
On 03/01/2015 18:15, WolfRage wrote: On 01/03/2015 06:58 AM, Dave Angel wrote: self.transposed_grid = zip(*self.grid) zip() sounds confusing. But I am going to try this and see what it gives me. But Somehow I think it will require me to understand yield, which I still do not totally get how to use. Also from the documentation, will this work on my 2d List given that the lengths of the lists may not be the same? IE: 4 columns and 8 Rows. The best way to answer this type of question is to try it yourself at the interactive prompt, it's a must have tool for any budding Python programmer. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Improving My Simple Game Code for Speed, Memory and Learning
Apologies; haven't had time to look at this thread carefully yet. Busy start of the new year. :P Minor comment: you can improve this snippet: if total == 17 or total == 21 or total == 28 or total == 29 or \ total == 31 or total == 42 or total == 45 or total == 46 \ or total == 49 or total == 58: by testing to see if total is an element in a sequence: if total in (17, 21, 28, ...): which you can read as: "If 'total' is a member of the tuple (17, 21, 28, ...)" This will let you avoid having to write so much to check that condition. The other comment I'd make is to start thinking about how you'd _test_ your program automatically. There's enough non-trivial logic that I'm not convinced that it actually does what you think it does. But then again, I haven't read it closely, so maybe it's trivial. Even so, tests help by acting as regression detectors: you have more freedom to start changing implementation, and the tests will catch mistakes for you. By "tests", I mean the kind of thing presented by: http://www.diveintopython3.net/unit-testing.html Good luck to you! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] How to get value of sublist as return to verify in other file
Hi All, I need help for list. I am having one list of list list = [[1, 2, 3, 4], [1, 5, 9, 11,5], [1, 6, 7, 2]] In this list i am getting sublist value as shown below: >>> list = [[1,2,3,4,5],[2,3,4,4,6],[3,4,5,4,6],[1,4,5,4,8]] >>> for sublist in list: ... if sublist[3] == 4: ...print sublist[3] ... 4 4 4 4 >>> I want sublist[3] value when matched first time and don't want the value for second match. And also i want to return same sublist value i.e. sublist[3] and want to get the value of sublist[3] in another file. Please provide any solution how to do this. thanks all in advance. Regards, Shweta ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to get value of sublist as return to verify in other file
On Mon, Jan 05, 2015 at 11:08:16PM +0530, shweta kaushik wrote: > Hi All, > > I need help for list. > > I am having one list of list > > list = [[1, 2, 3, 4], [1, 5, 9, 11,5], [1, 6, 7, 2]] > > In this list i am getting sublist value as shown below: > > >>> list = [[1,2,3,4,5],[2,3,4,4,6],[3,4,5,4,6],[1,4,5,4,8]] > >>> for sublist in list: > ... if sublist[3] == 4: > ...print sublist[3] > ... > 4 > 4 > 4 > 4 > >>> > > I want sublist[3] value when matched first time and don't want the value > for second match. I do not understand what you are asking. Do you mean that you only want to match once? py> list_of_lists = [[1, 2, 3, 4], [4, 5, 6, 7], [5, 2, 1, 4]] py> for sublist in list_of_lists: ... if sublist[3] == 4: ... print(sublist) ... break ... [1, 2, 3, 4] py> > And also i want to return same sublist value i.e. sublist[3] and want to > get the value of sublist[3] in another file. I am sorry, I have no idea what you are asking. Can you show some sample code and data that demonstrates what you are trying to do? -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to get value of sublist as return to verify in other file
On 05/01/15 17:38, shweta kaushik wrote: list = [[1,2,3,4,5],[2,3,4,4,6],[3,4,5,4,6],[1,4,5,4,8]] for sublist in list: ... if sublist[3] == 4: ...print sublist[3] ... 4 4 4 4 I want sublist[3] value when matched first time and don't want the value for second match. You need to break out of the for loop. The command to do that is 'break' Just add a break on a line after the print statement And also i want to return same sublist value i.e. sublist[3] and want to get the value of sublist[3] in another file. I'm not sure I understand what you mean there. If you break out of the loop you can still access your loop variable. But I'm not sure what you mean by "get the value of sublist[3] in another file." Do you want to read another file and compare it to sublist[3]? Or do you want to put the value of sublist[3] into another file? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor