Django - run a function every x seconds
I'm working on a Django app. I have an API endpoint, which if requested, must carry out a function that must be repeated a few times (until a certain condition is true). How I'm dealing with it right now is -
def shut_down(request): # Do some stuff while True: result = some_fn() if result: break time.sleep(2) return True
While I know that this is a terrible approach and that I shouldn't be blocking for 2 seconds, I can't figure out how to get around it.
This works, after say a wait of 4 seconds. But I'd like something that keeps the loop running in the background, and stop once some_fn returns True. (Also, it is certain that some_fn will return True)
EDIT -
Reading Oz123's response gave me an idea which seems to work. Here's what I did -
def shut_down(params): # Do some stuff # Offload the blocking job to a new thread t = threading.Thread(target=some_fn, args=(id, ), kwargs={}) t.setDaemon(True) t.start() return Truedef some_fn(id): while True: # Do the job, get result in res # If the job is done, return. Or sleep the thread for 2 seconds before trying again. if res: return else: time.sleep(2)
This does the job for me. It's simple but I don't know how efficient multithreading is in conjunction with Django.
If anyone can point out pitfalls of this, criticism is appreciated.
https://codehunter.cc/a/django/django-run-a-function-every-x-seconds
0 notes
ٹوئٹر نے ’حکومتی پروپیگنڈا‘ میں ملوث چھ ممالک کے 3500 اکاؤنٹس بند کر دیئے
ٹوئٹر نے ’حکومتی پروپیگنڈا‘ میں ملوث چھ ممالک کے 3500 اکاؤنٹس بند کر دیئے
ٹوئٹر نے ’حکومتی پروپیگنڈا‘ میں ملوث چھ ممالک کے 3500 اکاؤنٹس بند کر دیئے
نیویارک3، دسمبر ( آئی این ایس انڈیا )
مائیکروبلاگنگ سوشل میڈیا پلیٹ فارم ٹوئٹر کی انتظامیہ نے کہا ہے کہ اس نے چھ ممالک میں ایسے 3500 اکاؤنٹس بند کیے ہیں جن سے حکومت کے حق میں پروپیگنڈا کیا جا رہا تھا۔ خبر رساں ادارے اے ایف پی نے ٹوئٹر انتظامیہ کے بیان کا حوالہ دیتے ہوئے کہا کہ جن ممالک کے اکاؤنٹس بند کیے گئے ہیں ان میں…
View On WordPress
0 notes
Learning Python Basics Week #3
*information from codeacademy.com*
Used https://www.quora.com/What-is-difference-between-a-parameter-and-a-variable-in-programming-languages-like-Python-read-details as a resource
Functions:
If you’re trying to do an “if yes, say this” / “if no, say this” structure it as follows (s= type of shutdown):
def shut_down(s):
if s == “yes”:
return “Shutting down”
elif s == “no”:
return “Shutdown aborted”
else:
return “Sorry”
Importing Modules:
How to import the math module: type the following: import math
If you’re needing the square root, or any other specific math function, you can type the sequence as follows:
from math import sqrt
print squat( ) *inside the parenthesis, put the number of your choice
Quick review of : argument, function, and parameters
Function is like a paragraph, argument is like the sentence, parameters are like words.
Function Breakdown:
def________________( ) 1. Define the name of the function with the (parameter mane, ex. num)
if type ( ) == int or type( ) == float: 2. Type of if/or statement with int or float
return abs( ) 3. Give us the absolute value of ( )
else: 4. else, give us “Nope”
return “Nope”
Using Python to Plan Things
To calculate costs per night:
def hotel_cost(nights)
return 200 * nights
City/Cost: Structure as follows:
def plane_ride_cost(city)
if city == “city name”:
return 200
elif city == “city name”:
return 450
To Add Costs:
double(n) = double the return
triple(n) = triple the return
add(a, b) = gives sum of a + b
Lists:
Definition! Type of datatype- here is where you can store a collection of information as a sequence
Lists joins the group of datatypes we’ve already learned: strings, numbers, booleans
Format of a list:
list_name = [item_1, item_2]
Index:
Definition: gives item’s place on the list- helps you find an item on a list
Format: Use brackets! Remember, you begin lists at zero in python!
Add things to a list:
To get the number of things on a list, use:
len(list_name)
Formatting a list example:
grocery = [ ]
grocery.append(“apples”)
grocery.append(“salt”)
grocery.append(“carrots”)
grocery.append(“pizza”)
list_length = len(grocery)
print “There are %d items on the grocery” %(list_length)
print grocery
List Slicing:
Definition: When you only want part of a list
create a list named: letters
the subsection is stored in a slice
ex. letters[1:3]
this means the 1 is included in the element, but the 3 is excluded.
0 notes