Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am trying to figure out How to call function every 2 seconds in Python?
Should I use timer or thread?
Can anyone provide links/examples please?
The sched module can do that:
import sched, time
schedTimer = sched.scheduler(time.time, time.sleep)
def increaseX(x):
x += 1
print('X increased to ' + str(x))
schedTimer.enter(2, 1, increaseX, (x,))
schedTimer.enter(2, 1, increaseX, (3,))
schedTimer.run()
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 17 days ago.
Improve this question
I am trying to get the IP address and other details of Linux through my python code. when tmp = subprocess.call("./a.out") this line executes the code sticks here and this line subprocss.call("ifconfig") is not executed. as I cannot change the C code. so how can I make the next line run.
def execute():
dirname = '/home/kali/Downloads'
ext = ('.py','.c','.sh')
try:
for files in os.listdir(dirname):
if files.endswith(ext):
if files.endswith('.c'):
subprocess.call(["gcc", "code.c"])
tmp = subprocess.call("./a.out")
subprocss.call("ifconfig")
I got answer for this question after a long time of research. It can be done with a function subprocess.communicate function.
this function can communicate with the process.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
This is my first question, as I have started to code recently.
here it goes...
I dont know how to do it, so I haven't coded it yet.
The idea is to have a Conditional to try doing something for x seconds, and if nothing happens on this x seconds, then do something to try it again.
Like this..
Try for 5 seconds to:
click on element to download something # <- this I know how to do
if nothing happens: # <- no error, just not executed the line above
refresh the page
try again:
finally:
You have your file
sorry for my English, as it is not my primary language and I am also learning it...
what web scraping tool you are using? selenium ? I can only give you my
logic if you do not post your code.
import datetime
def page_refresh():
print('refresh page')
driver.refresh()
def check_and_wait():
status_ready = False
while (not status_ready):
start_time = datetime.now()
while(not status_ready and datetime.now()-start_time<5): # loop 5 seconds
if(condtion == True): # if something happen
status_ready = True
return
page_refresh() # nothing happen , loop again
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
one: A.ipynb
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
i want when i click on the button the result will be appear in the application.
flask application
from A import factorial
app = Flask(__name__)
#app.route('/')
def hello():
#return factorial(5)
return "Hello"
if __name__ == '__main__':
app.run()```
how can i solve it
You should use str to return number data.
return str(factorial (5))
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I'm creating TUI (Text-based user interface) using print statements, and when I want to return to the 'homescreen' i want the older code to run again.
randbool = True
while randbool:
print('1')
randbool = False
while not randbool:
print('2')
randbool = True
the result im expecting is
1
2
1
2
1
2
....
but it only prints 1, 2 how can I make it run indefinitely?
Not advisable, but:
while True:
print('1')
print('2')
This will print 1,2,1,2,1,2 indefinitely, until your CPU usage is at 100%, and your whole system freezes.
But it will accomplish what you're asking for.
Edit to add: 100% CPU usage demonstrated on an i7 laptop with 16GB RAM on Ubuntu 18.04:
If the value of randbool lets you get in a loop, changing it will stop the loop.
So don't change it for a loop you don't want to stop.
If you want to print 1 and 2 indefinitely, a simpler solution would be:
# loop forever
while True:
print('1')
print('2')
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm making The Powder Toy in Python, and I encountered some problems. Firstly the code runs VERY slowly. I know the problem is in my main file: http://pastebin.com/bbQ4H4Xu. The other files are just detecting input / creating the 2d array, so the problem isn't there.
Within my main file, the problem seems to be in the method updatescreen(). How can I increase the performance of this function?
import pygame
#inputkey.py
from pygame.locals import *
def input_key():
global inputt
inputt = ""
key = pygame.key.get_pressed()
if key[K_q]:
return 'q'
elif key[K_w]:
return 'w'
elif key[K_e]:
return 'e'
elif key[K_r]:
return 'r'
#Createblocks.py
blocks = []
for i in range(400):
blocks.append([])
for j in range(400):
blocks[i].append(0)
Your Main.py file has a print statement in the loop:
def updatescreen():
#The problem is here, it slows down the code.
for i in range(windh):
for x in range(windw):
print x, i # <== Here
if not blocks[i][x] == 0:
if blocks[i][x] == "Stone":
screen.blit(elementStone, (x,i))
presumably for debug? That's performing windw * windh = 2,500 print operations, which will slow the code down for sure. Try removing that and see how it improves.