Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: implement a general purpose call back method #23

Open
snowch opened this issue May 2, 2020 · 2 comments
Open

Feature Request: implement a general purpose call back method #23

snowch opened this issue May 2, 2020 · 2 comments

Comments

@snowch
Copy link

snowch commented May 2, 2020

It would be good to have a call back method that is just executed on each poll.

def after_each_poll(response):
    print('.')

polling.poll(
    lambda: requests.put('http://mysite.com/api/user', data={'username': 'Jill'},
    after_each_poll=after_each_poll,
    step=1,
    timeout=10)
@manvillej
Copy link

manvillej commented May 15, 2020

a bit of a work around, but you can get similar functionality with just a bit of abstraction around the collect_values.

def after_each_poll(response):
    print('.')

class CallBackQueue(Queue):
	"""calls the function when values are added to the queue"""
	def __init__(self, func):
		super(CallBackQueue, self).__init__()
		self.callback_func = func

	def put(response):
		self.func(response)
		Queue.put(response)

polling.poll(
    lambda: requests.put('http://mysite.com/api/user', data={'username': 'Jill'},
    after_each_poll=after_each_poll,
    step=1,
    collect_values = CallBackQueue(func=after_each_poll)
    timeout=10)

it works because of this line: https://github.com/justiniso/polling/blob/master/polling.py#L115

If you do not want to add the queue. simply do not extend the queue in your class.

class CallBackAbstraction(Object):
	"""calls the function when values are added to the queue"""
	def __init__(self, func):
		super(CallBackAbstraction, self).__init__()
		self.callback_func = func

	def put(response):
		self.func(response)

@manvillej
Copy link

alternatively, you could also do it at the check_success function: check the value, do your stuff, and then return a boolean.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants