0

Hi guys I'm new to Python and I can't find any approaches to my scenario . How can I start multiple repetitive tasks of the same function, with different parameters, and still have the ability to stop a certain one?

To make things clearer check the image bellow. I have an f(x) function. I want to start multiple independent recurring calls, with different entry data and at a later time stop them separately as well.

description http://happyfacedevs.com/public/python.png

UPDATE: Here's the whole scenario. I have a main function that gets called every 60 secs. This function gets a list of urls and based on some conditions it decides to start the repeating function with the given url or stops an already running one.

def f(x):
    #repeat every 5 secs until stopped
    #do stuff with x


def mainTask():
    #gets a new list of urls

    #for every url, start a new repeating task or stop an existing one
    #based on some conditions

    threading.Timer(60, mainTask).start()


mainTask()
3
  • Can you clarify "multiple repetitive tasks?" Do you mean that you want to call f(a), f(b), f(c) once each and have them run repeatedly until cancelled, or do you simply mean that you want to call f(a), f(b), f(c) and be able to stop any one of them at a later time? Commented Dec 5, 2013 at 8:28
  • To run things in parallel you can use Multiprocessing, or Multithreading. For repetitive tasks you need to loop (like a while loop) in order to be able to cancel them, you need a loop running on the main which will check for user stop command, which will notify the loops running on the functions to exit. These are 3 distinct different questions. Please ask a new question for each one, defining what you have tried, and what your goals are, include as much information/data as you can. Commented Dec 5, 2013 at 8:50
  • @sberry call every f(x) once and then have them recall themselves until stoped. There can be multiple (or even none) tasks running at a certain point in time. The 3 ones were only an example. Commented Dec 5, 2013 at 9:20

3 Answers 3

2

you can use gevent.spawn() to implement your independent tasks. a snippet from http://www.gevent.org/intro.html#example

>>>import gevent
>>>from gevent import socket
>>>urls = ['www.google.com', 'www.example.com', 'www.python.org']
>>>jobs = [gevent.spawn(socket.gethostbyname, url) for url in urls]
>>>gevent.joinall(jobs, timeout=2)
>>>[job.value for job in jobs]
['74.125.79.106', '208.77.188.166', '82.94.164.162']
Sign up to request clarification or add additional context in comments.

Comments

0

This is a very (too) broad question without showing any specific examples and there are many ways to achieve what you want. However, it might be worth looking at PEP 3156 and tulip for a high-level solution.

Comments

0

If a,b,c is constant all time:

import time
import threading

def f(param, stop_event):
    while not stop_event.is_set():
         # do something with param


events = {'a': threading.Event(), 
          'b': threading.Event(), 
          'c': threading.Event(), 
}
for param, ev in zip([a, b, c], events):
    t = Thread(target=f, args=(param, ev))
    t.start()
time.sleep(10)
events['a'].set() # stop a
time.sleep(10)
events['b'].set() # stop b
time.sleep(10)
events['c'].set() # stop c

If a,b,c new in iteration you can use Queue:

import time
import threading

def f(q, stop_event):
    while not stop_event.is_set():
         param = q.get()
         # do something with param

q = threading.Queue()
events = {'a': threading.Event(), 
          'b': threading.Event(), 
          'c': threading.Event(), 
}
for ev in events:
    t = Thread(target=f, args=(q, ev))
    t.start()
q.put(a)
q.put(b)
q.put(c)
time.sleep(10)
events['a'].set() # stop a
time.sleep(10)
events['b'].set() # stop b
time.sleep(10)
events['c'].set() # stop c

This is some sample solutions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.