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 gives them *args
.
I need to find a way to start a do_actionX()
function randomly 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 a function randomly I thought I can use a dictionary with functions inside and pick one of them randomly.
I think I can do this with threads, but since my bot is using multiprocessing, would it be a right choice to 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
The times that a function will be paused must be random. Is there a way to do this?
Are threads the right approach to this issue?