Efficient Way of Having a Function Only Execute Once in a Loop

How to run function only once in a loop and infinitely?

I would probably just pull the code that starts the app out of the while loop altogether, then replace the while loop with an infinite sequence of repeating bots.

from itertools import cycle


# Iterate through applications *once*, starting the app
# and creating the related bot name for future use.
bots = []
for app in applications:
bot = app.split(':')[1].strip()
print('Going to bot: ' + str(bot))
app = Desktop(backend="uia").windows(title=app)[0]
app.set_focus()
await asyncio.create_task(first_start(app_name=bot))
bots.append((app, bot))

# Cycle through the bots in one loop rather than restarting
# the loop in an infinite loop.
for app, bot in cycle(bots):
app.set_focus()
await asyncio.create_task(connect_wallet(app_name=bot))
await asyncio.create_task(login_metamask(app_name=bot))
await asyncio.create_task(treasure_hunt_game(refresh_only=True, app_name=bot))
await asyncio.create_task(new_map(app_name=bot))
await asyncio.create_task(skip_error_on_game(app_name=bot))

Effective way having a statement execute only once in an infinite function?

A simple answer would be to use a set to keep track of which values have already been seen:

seen = set()

def function():
if a in seen:
return
else:
seen.add(a)

if a == 1:
// do stuff for 1 once

if a == 32:
// do stuff for 32 once

if a == 10:
// do stuff for 10 once

while True:
function()

What is the most efficient way to execute code if a while loop runs at least once?

It seems you mean

if ( condition )
{
while(condition) {
doSomething();
}
doSomethingElse();
}

Or

if ( bool b = condition )
{
while( b) {
doSomething();
b = condition;
}
doSomethingElse();
}

or

if ( condition )
{
do {
doSomething();
} while ( condition );
doSomethingElse();
}

How to execute a piece of code only once?

Use global static objects with constructors (which are called before main)? Or just inside a routine

static bool initialized;
if (!initialized) {
initialized = true;
// do the initialization part
}

There are very few cases when this is not fast enough!


addenda

In multithreaded context this might not be enough:

You may also be interested in pthread_once or constructor function __attribute__ of GCC.

With C++11, you may want std::call_once.

You may want to use <atomic> and perhaps declare static volatile std::atomic_bool initialized; (but you need to be careful) if your function can be called from several threads.

But these might not be available on your system; they are available on Linux!

How to make an if inside a for-loop run only once?

You can add a Boolean value that is initialised to false and is then set to true directly after b.append(3.5) is run. If you check this value in the if statement it will only run once.

a = [1, 2, 3, 4, 5]
A = [4, 5]

b = []

ran = False
for i in a:
if i in A and not ran:
b.append(3.5) # My aim is to make this line run only once
ran = True
b.append(i)
else:
b.append(i)
print(b)

Is is possible to make a method execute only once?

Sure!..

if(!alreadyExecuted) {
doTrick();
alreadyExecuted = true;
}

How to run code inside a loop only once without external flag?

It's fairly hacky, but as you said it's the application main loop, I assume it's in a called-once function, so the following should work:

struct RunOnce {
template <typename T>
RunOnce(T &&f) { f(); }
};

:::

while(true)
{
:::

static RunOnce a([]() { your_code });

:::

static RunOnce b([]() { more_once_only_code });

:::
}


Related Topics



Leave a reply



Submit