I'm learning python and I'm doing some experiment with the module graph_tool. Since the function all_circuits could take a long time to calculate all the cycles, is there a way to stop the function (for example after "X" seconds or after the iterator reaches a certain size) and continue the execution of the script? Thanks
Asked
Active
Viewed 56 times
1
-
1if you will run code in separated Process then you can terminale/kill it - but it can make other problems because it needs to send all data to other process and someone get results. Processes don't share data and objects. Using `threading` you could share data but it doesn't have method to terminate/kill it and it need special methods. Similar problem with killing process/thread was few times in last few days. [python - Is there any way to kill a Thread? - Stack Overflow](https://stackoverflow.com/questions/323972/is-there-any-way-to-kill-a-thread) – furas Aug 27 '22 at 13:37
1 Answers
1
This is quite simple, actually. The function all_circuits()
returns an iterator over all circuits. Therefore, if you want to stop early, all you need is to break the iterations:
g = collection.ns["football"]
for i, c in enumerate(all_circuits(g)):
if i > 10:
print(c)
break
which prints
[ 0 1 25 24 11 10 5 4 9 8 7 6 2 3 26 12 13 15
14 38 18 19 29 30 35 34 31 32 21 20 17 16 23 22 47 46
49 48 44 45 33 37 36 43 42 57 56 27 62 61 54 39 60 59
58 63 64 100 99 89 88 83 53 52 40 41 67 68 50 28 69 70
65 66 75 76 95 87 86 80 79 55 94 82 81 72 74 73 110 114
104 93]
and stops.

Tiago Peixoto
- 5,149
- 2
- 28
- 28