People who are accustomed to Android programs try multithreading with Python

If you're new to Python and usually program Java on Android, you might think, "Would you like to set up a thread, then you have to handle the message with Handler?" However, there is no Python compatible version, so I was worried for a moment. This is a memo (small story) for Python beginners.

reference: http://qiita.com/konnyakmannan/items/2f0e3f00137db10f56a7 http://qiita.com/tortuepin/items/69fa0a307ebf15348885

The following is the one that was originally developed with Python2.7 and has a Python2.7 flavor. (I try to use six as much as possible)

Somehow, it is a correspondence table. (It's just a table written with a sense, and it doesn't have a deep meaning.)

Android Python Remarks
Looper None(with a while loop)
Handler Queue In Python3, queue. six.moves.Even queue is ok
notify threading.Event Android or Java

Queue

http://docs.python.jp/2/library/queue.html

import six.moves.queue as queue
import six.moves._thread as thread
import time


def target(q):
  while True:
    print "loop"
    print q.get()
    # q.task_done() #Notifies that the task obtained immediately before is completed. Not required if you don't use join
    time.sleep(0.1)

q = queue.Queue()
thread.start_new_thread(target, (q,))

while True:
  q.put("task")
  # q.join() #With join, in the queue, for all items, task_done()Wait to be done

It's like that.

theading.Event

If you just want to wait, use theading.Event.

import threading
import six.moves._thread as thread
import six
import time


def target(event):
  for cnt in six.moves.range(0, 10):
    print "loop:", cnt
    time.sleep(0.1)
  event.set()

event = threading.Event()
thread.start_new_thread(target, (event,))
event.wait()
print "done"

Recommended Posts

People who are accustomed to Android programs try multithreading with Python
Links to people who are just starting data analysis with python
Try to operate Facebook with Python
Try to reproduce color film with Python
To people who are "recruiting but not recruiting"
Try logging in to qiita with Python
Try to solve the man-machine chart with Python
Try to draw a life curve with python
Try to make a "cryptanalysis" cipher with Python
Try to automatically generate Python documents with Sphinx
Try to make a dihedral group with Python
[Python] Mention to multiple people with Slack API
Try to detect fish with python + OpenCV2.4 (unfinished)
Try scraping with Python.
[First API] Try to get Qiita articles with Python
Try to make a command standby tool with python
Try to solve the internship assignment problem with Python
Try to operate DB with Python and visualize with d3
I want to tell people who want to import from a higher directory with Python direnv
For those who want to write Python with vim
For those who are new to programming but have decided to analyze data with Python
Try to automate pdf format report creation with Python
Machine learning A story about people who are not familiar with GBDT using GBDT in Python
Try to display various information useful for debugging with python
[AWS] Try adding Python library to Layer with SAM + Lambda (Python)
Try to bring up a subwindow with PyQt5 and Python
Try to automate the operation of network devices with Python
It's Halloween so I'll try to hide it with Python
Try to decipher the garbled attachment file name with Python
Try to factorial with recursion
Connect to BigQuery with Python
Try to understand Python self
Try Python output with Haxe 3.2
Connect to Wikipedia with Python
Post to slack with Python 3
Try running Python with Try Jupyter
Switch python to 2.7 with alternatives
Write to csv with Python
Try face recognition with Python
Introducing how to use argparse for people who are laid back with as little effort as possible
Try to create a python environment with Visual Studio Code & WSL
Try to extract a character string from an image with Python3
Try to display google map and geospatial information authority map with python
Try to make capture software with as high accuracy as possible with python (1)
Try to solve the shortest path with Python + NetworkX + social data
Try to get CloudWatch metrics with re: dash python data source
Try adding a wall to your IFC file with IfcOpenShell python
[Python] Try to recognize characters from images with OpenCV and pyocr
Let's get started with Python 3. ~ For kittens who are wondering what to choose as their main language ~
Try scraping with Python + Beautiful Soup
Python: How to use async with
[Python] Write to csv file with Python
Create folders from '01' to '12' with python
Nice to meet you with python
Try singular value decomposition with Python
Output to csv file with Python
Try to profile with ONNX Runtime
Convert list to DataFrame with python
MP3 to WAV conversion with Python
To do tail recursion with Python2
Try to calculate Trace in Python