I am starting a loop in python, and after I start the loop I want the execution of code after the loop to carry on, while the loop keeps looping (in the 'background').
x=True
while x:
#do some output
sleep(1)
#ask for input or something
if input()=='something':
x=False
So in that example, #do some output will keep happening while input is asked for.
Is this possible in python? Are there any work-arounds that could achieve this?
From what I understand, what you want is to create a Thread that keeps executing some tasks in the background.
http://docs.python.org/library/threading.html
Threading is a moderately complex issue, so giving you a recipe here wouldn't amount to much. I suggest you take a look at the documentation to understand what's involved.
Update your question or create a new one if you then have troubles with something specific to the implementation.
EDIT:
I agree with the comment made by Tim Hoffman, the correct solution depends on what you're trying to achieve. From my understanding, threading should work for you, but if you give us more details, it might be easier to give a more precise answer.
Following OP's wish I'm posting the answer. :) There are multiple libraries for achieving what you are trying to do. Most known include:
threading module
multiprocessing module
There are subtle differences between them, but I guess that you shouldn't worry about it at the moment. I encourage you though to read more about mutli-processing and threading in general. Also "green" threads is an interesting alternative.
Related
I have been searched the answer for a while. The answers include using Ipyparallel, but there are no clear instructions on how can I do to apply to two cells in general. Most of the examples are just compute some value with some functions distributively. They are at least not clear for me to understand how to solve the problem. I wish someone could provide some code or instructions on how can I do for running two independent cells in general on Colab. Or, if there are other methods, it is also fine as long as it works. Thank you.
It's not exactly straightforward, because Python is synchronous by default. Maybe encapsulating your cells in functions and using asyncio to execute two functions asynchronously will do the job. Or, if it includes something like heavy processing, threading or multiprocessing modules may be the way to go.
How, after creating a user_input question and when inputting a response to the console, can I get the question to ask again, without having to run the script again?
Would I use a for/while loop for this? I have had a look around and tested some stuff out but I get errors or it doesn't work at all. I'm fairly new to programming. Thank you!
Yes you should use a while loop for this.
Keep in mind that to learn programming, you have to move forward one small step at a time, especially in the beginnings.
So if you want to use a while loop, learn to use it separately in another tutorial. Only when you're confortable with 'while' and 'input / print', try to combine both.
If you keep this mentality for everything during your learning, you should grow fast !
Good luck !
Im trying to create an upgrader/tycoon game and realised to have the money continuously going up, I would have to start a different process so I can have my GUI which can be used as the money goes up without either stopping the other from working.
What is the best way to do this?
I've looked around on this site and nothing I could find has helped me!
(If you need any more information just let me know!)
Use threading.
Python has a module named threading, and you need
threading.Thread(target=somefunc).start()
My old answer in the same topic: Here.
If you consider adding some code to your question, I am happy to help in threading! Also, if you need help in designing the code, feel free to ask, threads can be messy.
This seems like a somewhat simple question that I'm having a lot of trouble finding an answer for, perhaps I haven't found the words programmers use to talk about this.
I am a relatively inexperienced programmer, and have run into some difficulties with troubleshooting an application I have made. The question I have could be general, because I haven't found an answer to this for any language, but for me I'm specifically using python and PyQt4:
Is there any way to view the behind-the-scenes calls made when I execute a method/call a command? I can use debugging software (in my case winpdb) to track which lines in my code are called, but, aside from traceback info given after an error, haven't figured out how to follow the program's steps "behind the scenes". This would be helpful for cases where there hasn't been a programming error from the compiler's point of view, but the behavior is unexpected because the programmer doesn't have a complete understanding of a module's inner workings.
To put it another way: I want to know about the code that I didn't write. I want to know what is triggered after a line of my code is called. If I call a method like len(), python calls its own methods that will eventually return an integer to me. I'm hoping there's a way we can see what python does in between my lines of code.
If this has been asked before, please let me know, and accept my apologies for repeating the question, but I haven't been able to find an answer to that question, or at least the best way of asking it. Thank you very much for your help.
I'm not sure what your requirements are, but using cProfile with Gprof2Dot will generate a png of your code's call graphs and the amount of time spent in each function/method.
Just:
grab gprof2dot.py
Use the given sample code
I have a GUI driven image analysis package in IDL that needs some serious rewriting. Python has been suggested to be as an alternative to IDL (with benefits of cost and some nice libraries among other things). I've poked around now with PyQT4 and it looks like it should work nicely. However, one of the best things about IDL (being interpreted) is that if the code hits a bug, you can correct it on the fly, type 'retall' and then continue with your work. If you are hours into some analysis and have lots of datafiles open, etc., this is a HUGE improvement over having to exit, then change and restart the program. Not only that but we can quickly try some things on the command line, then if it looks good, code up a routine, put it in the menu structure, and then 'retall' and we are back with the new functionality, all without ever having to restart the program.
My question is, is this possible with Python? A little googling makes the answer seem like no but since it is an interpreted language I don't understand why not. If the answer really is no I'd strongly urge someone to think about implementing this -- it is probably the feature that made me most happy about IDL over the past decade.
Thanks in advance,
Eric
I don't think it can be done.
However, you may consider structuring your package to cache intermediate results on disk and to allow resume from thoses cached results. This has benefits outside the bug case you describe.
Fixing a bug "on the fly" seems potentially dangerous to me as it is so easy to overlook possible side-effects of the bug. It breaks the link between execution flow and code and make it extremely difficult to debug subsequent issues, etc ...