mercredi 2 décembre 2015

Pause and resume a function

I'm writing a bot in python and to "humanize" it I need to pause and resume functions randomly. Functions can be paused and resumed only at some defined points.

The bot is made of various functions, like

do_action1(*args)
do_action2(*args)
do_action3(*args)
...
start_bot()
stop_bot()

The function start_bot() calls do_action1(), do_action2(), ... in order and give them *args. I need to find a way to start randomly a do_actionX() function and at some points pause it and run another random do_actionX() function then pause it and resume the previous one and so on...

To start randomly a function I thought I can use a dictionary with functions inside and pick randomly one of them.

I think I can do this with threads, but since my bot is using multiprocessing, would it be a right choice use multithreading and multiprocessing together? I use multiprocessing to run multiple bots at the same time and manage them from a main python script which is linked to an interface. Each bot instance connects to a different account.

If I use multithreading, how can I make the function stop at some defined points and not randomly?

For example:

def do_action1(*args):
     print("something")
     # do something else
     # <--- at this point the function could be paused
     print("something")
     # <--- at this pint the function cannot be paused!
     print("else")
     # <--- and here the function could be paused again

Times a function will be paused must be random. Is there a way to do this? Are threads the right approach to this issue?

Thanks!




Aucun commentaire:

Enregistrer un commentaire