def function():
while True:
...omission...(this function is repeated permanently)
i =0
while i < 4:
driver.execute_script("""window.open("URL")""")
driver.switch_to.window(driver.window_handles[-1])
time.sleep(1)
function()
time.sleep(1)
i += 1 #open new tab and run function.
it doesn't work because while true loop is repeated permanently. Is there any ways to run multiple functions together?
https://imgur.com/a/4SIVekS This picture shows what I want
According to your picture, what you want is to launch the function a set number of times (4?), and run those in parrallel.
On a single core, as is the normal behavior, straight up parallel processing is impossible. You need to access other cores and manage a decentralized processing. while is useless there. I'm worried the level of difficulty is over your current skills, but here we go.
The overall flow that you (probably, depends on the actual memory safety of your functions) need is:
- to create a thread pool with the set number of threads for the number of runs you want.
- indicate the function you need to run
- start them, making sure the start itself is non-blocking.
- ensure one functions's processing doesn't impact another's results. race conditions are a common problem.
- gather results, again, in a non-blocking way.
You can use several methods. I highly recommend you read up a lot on the following documentations.
Threading:
https://docs.python.org/3/library/threading.html
Multiprocessing:
https://docs.python.org/3/library/multiprocessing.html
I don't understand your question because I don't understand what your function is supposed to do.
while True:
will always create an infinite loop. "while" is a command that tells python to loop through the following block so long as the expression following it evaluates to True. True always evaluates to True.
It seems like you want to use a conditional, like you do in "while x < 4".
x < 4
...is an expression that evaluates to true when x is less than 4, and false if x is not less than 4. Everything below the line:
while x < 4:
will then run if x is less than 4, and when it's done running that code, it will go back and evaluate if x is less than 4 again, and if it is, run the code again. To include another while loop inside of that loop, that new loop also needs an expression to evaluate. If you want to evaluate the same expression, write it out:
while x < 4:
# do something
while x < 4:
#do more things
# do even more things
# change x at some point, or else you're in an infinite loop.
However, there's no reason to do that specifically, because you're already doing it. All of the code is only running when x < 4, so checking that condition again right there is redundant, and doing it in another loop doesn't make sense. If the inside loop is also incrementing x, then the outside loop won't loop and doesn't need to increment x.
Also, if you want a function to check a condition based on a variable outside the function, you'll want to pass things to that function.
Related
I have a breakpoint in my Python debugger. I am using PyCharm. I want to iterate lets say 100 times to reach the point where I want to debug.
Now I can press 100 x times Resume Program, but is there a way to just execute a command to run n times over the breakpoint.
You can use a function in a conditional breakpoint to count iterations, take for example:
The conditional breakpoint can call a function which in addition to returning a boolean, counts the number of loop iterations.
def your_counter(stop):
global count
count = count + 1
if stop == count:
# count = 0 for periodic break
return True
else:
return False
The solution shown is for cases when a one-liner condition might not be practical, and/or when a loop counter needs to be implemented externally. Since the breakpoint condition is programmatic you can implement it to break periodically, or on any series/frequency criteria you want to apply.
The custom condition would break at the exact iteration you want, after you're done "step debugging" either press resume, stop, "run to cursor", or disable the breakpoint right-clicking on it (in practice this gets you out of the loop).
You can also change the value of any variable in the middle of debugging by editing in "variable watches".
PyCharm offers the possibility to add conditions on specific breakpoint.
This feature is called Conditional Breakpoints and you can find the documentation here.
I think that this is what you are looking for, because in this way you are able to enable the breakpoint only under specific conditions.
I'm debugging some code in PyCharm by stepping through line-by-line. The code involves a long for-loop, but for debugging purposes, I don't need to iterate through the whole thing. Instead, I'd like to break out after a few iterations. I'm trying to figure out how to do so without modifying my code to have special debugging instructions.
Example for loop
indices = range(10000, 0, -1)
for i, val in enumerate(indices):
print(val) # I can tell that it's working after a few iterations
The first thing I tried was typing break in the debugging console when paused at the print(val) line. This resulted in a syntax error
SyntaxError: 'break' outside loop
I also thought about using the debugging console line to modify the variable that I am enumerating over. For example, entering
indices = []
This has no effect on the iterator. Apparently, the iterator has an independent copy of the variable.
In another language, Java for example, I would take advantage of the termination condition, but python loops don't have a termination condition. I could rewrite my for-loop as a while-loop so that it would have an explicit termination condition, but that would obfuscate the code.
The other option would be to add special debugging instructions. For example,
mode = 'debug'
indices = range(10000, 0, -1)
for i, val in enumerate(indices):
print(val) # I can tell that it's working after a few iterations
if mode == 'debug' and i % 10:
break
I dislike doing this because inevitably, one of these special instructions gets left in after debugging is finished and mucks everything up.
Do I have any other options to break out of the loop from the debug console or without modifying the code?
I dislike doing this because inevitably, one of these special instructions gets left in after debugging is finished and mucks everything up.
Python for loops just aren't designed to be changed during execution, besides hard coding break, return or throwing StopIteration. (You are debugging, so either change the range in the loops expression list, or let it run through). The reason being the advantages of straightforward simplicity outweigh exposing the for loop counter - see PEP 212.
I'm trying to ignore a specific breakpoint in pycharm for the first N times it hits. Since I'm looking to set it to something like 10k, manually doing this is not an option. I found the expanded options for breakpoints, including the condition field, but I'm not sure how I can craft a condition which takes into account how many times the breakpoint has been hit. Thanks.
You can just create a variable in Python specifically for the breakpoint counting purpose, which you increment every time you you go past the break point line. Then just use that variable in your break point condition (i.e. breakpoint_count == 10000).
Update
If you can't add new code into the real python code you can use the breakpoint condition:
eval("exec('try:\\n x += 1\\nexcept NameError:\\n x = 1') or x == 10000")
What this does is execute a try statement which increments a variable or creates it if it doesn't exist. Then evaluates that along with a statement checking if the variable has been incremented enough times yet with that being your ending condition. Note, the exec is required to run the try, but the eval is needed to "return" the condition to PyCharm. This is absurdly hacky, but it works for your case!
The following code hangs,
while np.any((np.log10((O3 / HB)) < 0.61 / (np.log10(N2 / HA) - 0.05) + 1.3).any()):
plt.plot(np.array(np.log10(N2 / HA)), np.array(np.log10(O3 / HB)), '.g')
plt.plot(np.array(np.log10(N2 / HA)), np.array(np.log10(O3 / HB)), '.r')
The plot works without the "while" condition. In the above form it hangs.
Do I need a "break"?
thanks
The code probably freezes because you are making a new plot object every time the loop iterates, eventually you'll run out of memory. The program usually spits out a warning after 20 or so plots made.
You can clear the axis each time as an alternative, or
When to use cla(), clf() or close() for clearing a plot in matplotlib?
or you could use the .set_xdata, set_ydata commands to simply update the data. Or you could reuse the same axes if you want to overlay the data.
Discussion of Issue
The code freezes because you are using a while loop with a condition whose variables do not change within the loop. Therefore, if the condition evaluates to True the first time through the loop, it will evaluate to True indefinitely, (i.e. the code 'hangs'). Typically (unless you want the loop to run forever) one or more variables within a while loop will change within the loop. A very simple example is,
while index < 10:
plt.plot(x_data[index], y_data[index])
index += 1 # Here the index changes.
Note: For these purposes it is more common to use for index in range(10): or better yet for xd, yd in zip(x_data, y_data): plt.plot(xd, yd) rather than a while loop because then you don't need to have the index += 1 line, and it is clearer at the beginning of the loop what is going on. I merely provided this as a clear example use of while.
Even if the conditional variables do change within the loop (i.e. if you're only providing a snippet of your while loop), it is possible that the conditional statement always evaluates to True. In this case, again, you will never exit the while loop and your code will 'hang'.
Potential Solutions
There are several ways you could fix your problem depending on what you are trying to do:
If you just want to plot your data once, use if instead of while.
If you are only showing us a snippet of your loop and the variables in the conditional statement do change within the loop, I would recommend:
A) starting by placing a print(np.any((np.log10((O3 / HB)) < 0.61 / (np.log10(N2 / HA) - 0.05) + 1.3).any())) at the top of your loop and run your code. I presume this will dump True to your console.
B) You could then start to break this statement apart using more print statements such as print('N2: %f' % N2) to understand why your statement is never evaluating to False and therefore you are never exiting the loop.
This question already has answers here:
More Pythonic Way to Run a Process X Times [closed]
(5 answers)
Closed 7 months ago.
In Python you have two fine ways to repeat some action more than once. One of them is while loop and the other - for loop. So let's have a look on two simple pieces of code:
for i in range(n):
do_sth()
And the other:
i = 0
while i < n:
do_sth()
i += 1
My question is which of them is better. Of course, the first one, which is very common in documentation examples and various pieces of code you could find around the Internet, is much more elegant and shorter, but on the other hand it creates a completely useless list of integers just to loop over them. Isn't it a waste of memory, especially as far as big numbers of iterations are concerned?
So what do you think, which way is better?
but on the other hand it creates a completely useless list of integers just to loop over them. Isn't it a waste of memory, especially as far as big numbers of iterations are concerned?
That is what xrange(n) is for. It avoids creating a list of numbers, and instead just provides an iterator object.
In Python 3, xrange() was renamed to range() - if you want a list, you have to specifically request it via list(range(n)).
This is lighter weight than xrange (and the while loop) since it doesn't even need to create the int objects. It also works equally well in Python2 and Python3
from itertools import repeat
for i in repeat(None, 10):
do_sth()
python3 & python2
just use range():
for _ in range(n):
# do something n times exactly
The fundamental difference in most programming languages is that unless the unexpected happens a for loop will always repeat n times or until a break statement, (which may be conditional), is met then finish with a while loop it may repeat 0 times, 1, more or even forever, depending on a given condition which must be true at the start of each loop for it to execute and always false on exiting the loop, (for completeness a do ... while loop, (or repeat until), for languages that have it, always executes at least once and does not guarantee the condition on the first execution).
It is worth noting that in Python a for or while statement can have break, continue and else statements where:
break - terminates the loop
continue - moves on to the next time around the loop without executing following code this time around
else - is executed if the loop completed without any break statements being executed.
N.B. In the now unsupported Python 2 range produced a list of integers but you could use xrange to use an iterator. In Python 3 range returns an iterator.
So the answer to your question is 'it all depends on what you are trying to do'!