2

I'm new to python, so please excuse my ignorance. I am trying to get a process to run at the same time as my main file. The use case I have is that I want to alter the points of a game (adding/adjusting points for all users) at the same time that my flask/python app accepts CRUD requests. I could probably just schedule this for a midnight run or something, but in the future I may want to make multiple changes to the points depending on user input. Basically though, I really want to use some sort of threading feature.

Unfortunately, my thread blocks the operation of main. I'm not sure why, as I had thought that the threading's whole point was that it ran at the same time.

Here is how I call my function from main:

i = Inflate('pants')
i.timermethod()

Here is the class and method as I have defined them:

from flask_restful import abort, reqparse, Resource
from marshmallow import Schema, fields, ValidationError, pre_load
from flask import Flask, Blueprint, request, jsonify
from flask_cors import CORS, cross_origin
import psycopg2
import os
from os.path import join, dirname
import threading
from time import sleep

class Inflate:
    def __init__(self, s):
        self.s = s
    def printtest(self):
        print('insided the printtest for inflation')
    def hello(self, h):
        print h + self.s
    def timermethod(self):
        h="hello there "
        for i in range(5):
            t = threading.Thread(target=self.hello, args=(h,))
            t.start()
            sleep(2)

The output is that "hello there pants" is printed 5 times before my main function executes, whereas I would expect/want "hello there pants" to be printed maybe once, see other output from main as it runs at the same time, and then "hello there pants" to continue executing.

Please let me know if you have any ideas, I am stuck.

6
  • you sleep for 2 after each thread start so each of them will be done by the time you start next, remove sleep and you may see results that are more consistent with your expectations, but then again your threads do so little that they will finish before you start another so you will still likely see 5 'hello' Commented Jul 14, 2017 at 16:24
  • ok, but then how do i set a wait condition every n seconds on the thread that doesnt block main? I need to wait for some time period before executing another thread somehow. Commented Jul 14, 2017 at 16:27
  • why do you need to wait? Commented Jul 14, 2017 at 16:28
  • i want a function that runs every 10 minutes and does something (messes with the points in my game). So I need a wait function for the the spawned processes (but that wont block main). Commented Jul 14, 2017 at 16:29
  • then spawn just one thread and wait inside the thread Commented Jul 14, 2017 at 16:29

2 Answers 2

2

Sleep blocks. You need to execute timermethod from a separate thread.

try:

t = Thread(target=i.timermethod)
t.start()

print "i will print immediately"

# print test will run 5 times in 5 separate threads, once every 2 secs

instead of:

i.timermethod() 

# print test will run 5 times in 5 separate threads, once every 2 secs

print "i have to wait for timermethod() to finish"

#code that gets blocked

from your main thread. You need to explicitly tell python to invoke timermethod in its own thread, otherwise it will run in main.

Sign up to request clarification or add additional context in comments.

3 Comments

ok......but that doesn't solve the problem of wanting to have some time between execution cycles. If I just execute the thread again it will immediately run it. I need a "non blocking sleep".
No, it does - by running timermethod in its own thread, "sleep" runs, and therefore blocks, in a separate thread rather than you main thread. From its own thread, you can sleep however you want.
in your code, you are invoking sleep, and therefore blocking, in your main thread. hope that makes sense
1

You call i.timermethod() which sleeps for 2 seconds 5 times before returning.

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.