Reputation: 195
I know the question seems very weird, but maybe this example can lay it out more clearly.
def entry_point():
if list_already_exists:
list.append(something)
else:
list = [something]
run_program(list)
def run_program(list):
for idx, value in enumerate(list):
print(idx)
time.sleep(value)
So run_program
is the actual function which does everything(kind of like the driver code)
entry_point
can be called many times even when run_program
is under execution.
With the current approach, whenever I try to modify the list
, run_program
starts all over again from the beginning.
I don't want that. I want entry_point
to execute run_program
just once and next time I run entry_point
to modify list
, run_program
shouldn't be interrupted, only the updated list should be passed to run_program
.
Intended output
first ever call to
entry_point()
should create a new list, lets assumelist = [1]
entry_point() --> run_program(list) output would be
1
andwait
for 1 sec
second call to
entry_point()
adds something to the already existing list.list = [1,2]
meanwhile, after one second wait,
run_program(list)
would print2
andwait
for 2 secs
So on and so forth...
Any help would be much appreciated..
Upvotes: 0
Views: 320
Reputation: 282
I agree with others that the behavior you described is more suited to listers, event emitters or threads. However, I am still unsure of the exact use case you need it for. Following is a simple implementation without any of the mentioned things - that satisfies the flow you mentioned. You can tweak it according to the need if it fits your constraints.
All calls to the entry_point also pass a index to the run_program function and hence the global list is updated and each time the correct value is chosen for the sleep function. Hope it helps to some extent.
import time
global val
val = 1
global lst
def entry_point():
global val
global lst
try:
lst.append(val)
except:
lst = [val]
idx = lst.index(val)
run_program(lst, idx)
val += 1
def run_program(lst, idx):
value = lst[idx]
print(f'Index is {idx} and value is {value}')
time.sleep(value)
entry_point()
entry_point()
Upvotes: 1
Reputation: 399
To me it sounds like you need some kind of Thread to keep run_program executing while you modify the list in the entry_point function.
You can pass the list in a Queue and make the run_program function continue in the point it left the previous list.
Upvotes: 0