Asynchronous processing (threading) in python

import threading
import time
import sys
def f():
    '''
Processing you want to do asynchronously
This time, the number of seconds is displayed every second.
    '''
    i = 1
    while True:
        print(i)
        i += 1
        time.sleep(1)

th = threading.Thread(target=f,name="th",args=())
#Creation of thread th Method to be performed by target,The name of the thread in name,Specify arguments with args
th.setDaemon(True)
#Set th to daemon. When the main thread terminates, the daemon thread also terminates
th.start()
#Start thread

#Accepts character input, ends if a
#Since it is the main thread, when this ends, the daemon thread th also ends.
while True:
    c = sys.stdin.read(1)
    if c == 'a':
        sys.exit()

I am creating a thread called th that displays the number of seconds every second. Exit when'a'is entered.

Recommended Posts

Asynchronous processing (threading) in python
File processing in Python
Multithreaded processing in python
Text processing in Python
Queue processing in Python
Asynchronous processing in Python: asyncio reverse lookup reference
UTF8 text processing in python
Image Processing Collection in Python
Celery asynchronous processing in Flask
Using Python mode in Processing
Signal processing in Python (1): Fourier transform
100 Language Processing Knock Chapter 1 in Python
Asynchronous processing using LINE BOT: RQ (Redis Queue) in Python
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
python image processing
Meta-analysis in Python
Unittest in python
Asynchronous processing using Linebot in Job queue
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
Python file processing
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
Easy image processing in Python with Pillow
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Asynchronous processing implementation in Django (Celery, Redis)
Duplicate prohibition processing in GAE / Python Datastore
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Status of each Python processing system in 2020
Flatten in python
flatten in python