Forum Archive

how to understand thread clearly?

ryubai

how to understand thread clearly? see my comments
I run these code and return such results.


{number = 7, name = (null)}
{number = 1, name = main}
{number = 7, name = (null)}


import threading
from objc_util import  *
import ui

td=ObjCClass('NSThread')
print(td.currentThread())
#why isn't this in a mainthread? it shows in background thread.

#it shows in background thread,it's okay
@ui.in_background
def test1():
    print(td.currentThread())

#it shows in main thread,it's okay
@on_main_thread
def main():
    global td   
    print(td.currentThread())
    test1()

if __name__=='__main__':
    main()
JonB

https://forum.omz-software.com/topic/4528/help-me-understand-pythonista-s-threads

Basically to iOS, the only things non the main thread are user interface code and callbacks. Everything else is in the background queue. By default, there are just these two threads, and in_background gets queued onto the background thread -- meaning your main code must exit before anything from in_background will run. That leads to strange scenarios -- you just never have code in the main script that blocks waiting for something from a in_background function, since the in_background code won't execute until the main script is done. Also, never have blocking dependencies between two in_background items. It is often better to create your own thread if you plan on having code wait in the background for other conditions to be true.

Callbacks -- button presses, etc are called on the main thread.

ryubai

@JonB

thank you very much for detailed reply.
i will have more tests and try to understand it indepth.
maybe ask later when i meet new question,thank you again.