Question

First of all, here are my two python files:

sred.py:

import _thread,time

class Thread:

def __init__(self,time:int,say:str):

    self.time=time
    self.say=say


def create():
    id = _thread.get_ident() 


    for i in range(5):

        print("HALLO", id)

    return

from sred import Thread
import time,_thread

_thread.start_new_thread(Thread.create,())

The second one: main.py

from sred import Thread
import time,_thread

_thread.start_new_thread(Thread.create,())

when executing this it doesn't print anything out, why?

UPDATE:

import _thread

class Thread:


    @classmethod
    def create():
        id = _thread.get_ident() 


        for i in range(5):
            print("HALLO", id)
        return

main.py:

from sred import Thread
import time,_thread

_thread.start_new_thread(Thread().create,())

Is this now right, or is there still something wrong?

Was it helpful?

Solution

The create method is missing self as a parameter -- it looks like it should also be a @classmethod if you want to call it as it's written now. Note that your __init__ method is never getting called, because you never instantiate any Thread objects. You may want it to read:

_thread.start_new_thread(Thread().create, ())

i.e., instantiate a thread, then pass its create method to be executed in the new thread. I'm not sure what's happening, but I suspect that something is erroring and the stacktrace is being suppressed by something.

Also, you need to delete the space after the for statement -- it's significant, and it should be throwing you a syntax error about an unexpected indent.

EDIT:

This version runs on my machine:

import _thread

class Thread:
    def create(self):
        id = _thread.get_ident() 

        for i in range(5):
            print("HALLO", id)
        return

_thread.start_new_thread(Thread().create, ())
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top