Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8294607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:16:37+00:00 2026-06-08T14:16:37+00:00

So i have a thread: import threading, time, serial, logging class SerialThread(threading.Thread): # SerialThread

  • 0

So i have a thread:

import threading, time, serial, logging

class SerialThread(threading.Thread):

    # SerialThread class construcor
    def __init__(self, sleep):
        self.sleep = sleep
        threading.Thread.__init__(self,name = "SerialThread")
        self.setDaemon(1)

    # SerialThread method
    def run(self):

        # Do forever
        while 1:   

            # Sleep afther every loop
            time.sleep(self.sleep)
            print 'Doing some work!'

    def setSleep(self, sleep):
         self.sleep = sleep

And then in my main thread i do:

serialThread = SerialThread(60)
serialThread.start()

def changeSleep():
    serialThread.setSleep(80)

Im am starting this thread from my main code. But now when i want to change the self.sleep number what should i do? I have tried this simple and naive solution to just create a method in the thread class, and when its called it sets the sleep time. Apparently this didnt work as i expected.
A good example would be welcome.

EDIT: added the code i tried before

EDIT: So it wont work as the self.sleep is always 60 as i set it in the beginning, even afther i call the serialThread.setSleep(80)

EDIT: Didnt think this is relevant but im creating web service with flask and serialThread.setSleep(80) is called by accessing the flask route. So i guess flask runs also on separate thread and because of that serialThread.setSleep(80) is not actually called from the main thread…

Code:

@app.route('/api/setConf', methods=['POST'])
def setConf():

    serialThread.setSleep(config.interval)

    return 'ok'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T14:16:39+00:00Added an answer on June 8, 2026 at 2:16 pm

    Works for me with the following main body code (and a helpful print in the thread code):

    serialThread = SerialThread(3)
    serialThread.start()
    
    def changeSleep():
        serialThread.setSleep(6)
    
    time.sleep(15)
    changeSleep()
    time.sleep(15)
    

    Output:

    sleeping for 3
    Doing some work!
    sleeping for 3
    Doing some work!
    sleeping for 3
    Doing some work!
    sleeping for 3
    Doing some work!
    sleeping for 3
    Doing some work!
    sleeping for 6
    Doing some work!
    sleeping for 6
    Doing some work!
    sleeping for 6
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this class: from threading import Thread import time class Timer(Thread): def __init__(self,
from threading import Thread import time print 'start of script' class MyThread(Thread): def run(self):
Basically I have the following code: import multiprocessing import time class MyProcess(multiprocessing.Process): def __init__(self,
Say I derive from threading.Thread: from threading import Thread class Worker(Thread): def start(self): self.running
I have a thread that loops receiving socket data and printing it: def post(self):
I have a problem with Observer-Pattern and deadlock using threads. package observerDeadLock; import java.util.Observable;
I have thread where downloading xml file a this xml file i want to
Suppose I have thread 1, the main window UI thread and thread 2, a
Is if almost always required to have thread syncing (i.e. use of mutex, semaphores,
I have a thread which needs to be run in the background every 1000ms.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.