Python Flask Server generating too many new threads












1















I have a Python Flask server running on Python 3.6.6 that generates new threads for some reason. This is my code:



# Packages and Modules
import os
import threading, time
from config import config
from Modeller.Modeller import Modeller

# Job scheduler function
def run_job(Modeller, config):
while True:
print("Run recurring task")
print(threading.enumerate())
modeller = Modeller(config) # Initiate modeller with config
modeller.run()
time.sleep(modeller.config['settings']['delay'])

# Start thread for calling function repeatedly
thread = threading.Thread(name='modelling', target=run_job, daemon=True, args=[Modeller, config])
thread.start()


The first log generates:



[<_MainThread(MainThread, started 140381841954560)>, <Thread(modelling, started daemon 140381169248000)>]


But after a few runs it generates:



[<_MainThread(MainThread, started 140381841954560)>, <Thread(modelling, started daemon 140381169248000)>, <Thread(Thread-1, started daemon 140381017106176)>, <Thread(Thread-2, started daemon 140380804740864)>, <Thread(Thread-3, started daemon 140380796348160)>, <Thread(Thread-4, started daemon 140380786644736)>, <Thread(Thread-5, started daemon 140380778252032)>]


The only threads that should be listed are MainThread and modelling? What am I doing wrong?










share|improve this question

























  • How does this code relate to Flask? What is calling or importing this?

    – Daniel Roseman
    Nov 26 '18 at 10:29











  • I think you should provide the full code to show how this code used with Flask.

    – Bumsik Kim
    Nov 26 '18 at 10:46











  • Do you override the threading config of Flask? Do you start new threads in modeller.run?

    – Adonis
    Nov 26 '18 at 17:41
















1















I have a Python Flask server running on Python 3.6.6 that generates new threads for some reason. This is my code:



# Packages and Modules
import os
import threading, time
from config import config
from Modeller.Modeller import Modeller

# Job scheduler function
def run_job(Modeller, config):
while True:
print("Run recurring task")
print(threading.enumerate())
modeller = Modeller(config) # Initiate modeller with config
modeller.run()
time.sleep(modeller.config['settings']['delay'])

# Start thread for calling function repeatedly
thread = threading.Thread(name='modelling', target=run_job, daemon=True, args=[Modeller, config])
thread.start()


The first log generates:



[<_MainThread(MainThread, started 140381841954560)>, <Thread(modelling, started daemon 140381169248000)>]


But after a few runs it generates:



[<_MainThread(MainThread, started 140381841954560)>, <Thread(modelling, started daemon 140381169248000)>, <Thread(Thread-1, started daemon 140381017106176)>, <Thread(Thread-2, started daemon 140380804740864)>, <Thread(Thread-3, started daemon 140380796348160)>, <Thread(Thread-4, started daemon 140380786644736)>, <Thread(Thread-5, started daemon 140380778252032)>]


The only threads that should be listed are MainThread and modelling? What am I doing wrong?










share|improve this question

























  • How does this code relate to Flask? What is calling or importing this?

    – Daniel Roseman
    Nov 26 '18 at 10:29











  • I think you should provide the full code to show how this code used with Flask.

    – Bumsik Kim
    Nov 26 '18 at 10:46











  • Do you override the threading config of Flask? Do you start new threads in modeller.run?

    – Adonis
    Nov 26 '18 at 17:41














1












1








1








I have a Python Flask server running on Python 3.6.6 that generates new threads for some reason. This is my code:



# Packages and Modules
import os
import threading, time
from config import config
from Modeller.Modeller import Modeller

# Job scheduler function
def run_job(Modeller, config):
while True:
print("Run recurring task")
print(threading.enumerate())
modeller = Modeller(config) # Initiate modeller with config
modeller.run()
time.sleep(modeller.config['settings']['delay'])

# Start thread for calling function repeatedly
thread = threading.Thread(name='modelling', target=run_job, daemon=True, args=[Modeller, config])
thread.start()


The first log generates:



[<_MainThread(MainThread, started 140381841954560)>, <Thread(modelling, started daemon 140381169248000)>]


But after a few runs it generates:



[<_MainThread(MainThread, started 140381841954560)>, <Thread(modelling, started daemon 140381169248000)>, <Thread(Thread-1, started daemon 140381017106176)>, <Thread(Thread-2, started daemon 140380804740864)>, <Thread(Thread-3, started daemon 140380796348160)>, <Thread(Thread-4, started daemon 140380786644736)>, <Thread(Thread-5, started daemon 140380778252032)>]


The only threads that should be listed are MainThread and modelling? What am I doing wrong?










share|improve this question
















I have a Python Flask server running on Python 3.6.6 that generates new threads for some reason. This is my code:



# Packages and Modules
import os
import threading, time
from config import config
from Modeller.Modeller import Modeller

# Job scheduler function
def run_job(Modeller, config):
while True:
print("Run recurring task")
print(threading.enumerate())
modeller = Modeller(config) # Initiate modeller with config
modeller.run()
time.sleep(modeller.config['settings']['delay'])

# Start thread for calling function repeatedly
thread = threading.Thread(name='modelling', target=run_job, daemon=True, args=[Modeller, config])
thread.start()


The first log generates:



[<_MainThread(MainThread, started 140381841954560)>, <Thread(modelling, started daemon 140381169248000)>]


But after a few runs it generates:



[<_MainThread(MainThread, started 140381841954560)>, <Thread(modelling, started daemon 140381169248000)>, <Thread(Thread-1, started daemon 140381017106176)>, <Thread(Thread-2, started daemon 140380804740864)>, <Thread(Thread-3, started daemon 140380796348160)>, <Thread(Thread-4, started daemon 140380786644736)>, <Thread(Thread-5, started daemon 140380778252032)>]


The only threads that should be listed are MainThread and modelling? What am I doing wrong?







python python-3.x multithreading python-multithreading






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 10:33







danielo

















asked Nov 26 '18 at 10:25









danielodanielo

287215




287215













  • How does this code relate to Flask? What is calling or importing this?

    – Daniel Roseman
    Nov 26 '18 at 10:29











  • I think you should provide the full code to show how this code used with Flask.

    – Bumsik Kim
    Nov 26 '18 at 10:46











  • Do you override the threading config of Flask? Do you start new threads in modeller.run?

    – Adonis
    Nov 26 '18 at 17:41



















  • How does this code relate to Flask? What is calling or importing this?

    – Daniel Roseman
    Nov 26 '18 at 10:29











  • I think you should provide the full code to show how this code used with Flask.

    – Bumsik Kim
    Nov 26 '18 at 10:46











  • Do you override the threading config of Flask? Do you start new threads in modeller.run?

    – Adonis
    Nov 26 '18 at 17:41

















How does this code relate to Flask? What is calling or importing this?

– Daniel Roseman
Nov 26 '18 at 10:29





How does this code relate to Flask? What is calling or importing this?

– Daniel Roseman
Nov 26 '18 at 10:29













I think you should provide the full code to show how this code used with Flask.

– Bumsik Kim
Nov 26 '18 at 10:46





I think you should provide the full code to show how this code used with Flask.

– Bumsik Kim
Nov 26 '18 at 10:46













Do you override the threading config of Flask? Do you start new threads in modeller.run?

– Adonis
Nov 26 '18 at 17:41





Do you override the threading config of Flask? Do you start new threads in modeller.run?

– Adonis
Nov 26 '18 at 17:41












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53479085%2fpython-flask-server-generating-too-many-new-threads%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53479085%2fpython-flask-server-generating-too-many-new-threads%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Ottavio Pratesi

Tricia Helfer

15 giugno