2018/08/13

Python multiprocessing and thread


Thread

import threading
from datetime import datetime


def show(n, s):
    total = 0
    from random import random
    for i in range(n):
        total += i
    print('total={} start={} end={}'.format(total, str(s), str(datetime.now())))

if __name__ == "__main__":
    max = 1000

    jobs = []
    for index in range(4):
        s = datetime.now()
        jobs.append(threading.Thread(target=show, args=[max, s]))

    for index in range(4):
        jobs[index].start()

    for index in range(4):
        jobs[index].join()



total=499500 start=2018-08-13 14:32:26.588980 end=2018-08-13 14:32:26.590985
total=499500 start=2018-08-13 14:32:26.588980 end=2018-08-13 14:32:26.591987
total=499500 start=2018-08-13 14:32:26.589481 end=2018-08-13 14:32:26.592990
total=499500 start=2018-08-13 14:32:26.589481 end=2018-08-13 14:32:26.593993

Python install Parse Server API

先用Github fork「ParsePy」修改connection.py的24行



API_ROOT = os.environ.get('PARSE_API_ROOT') or 'https://api.parse.com/1'
修改成
API_ROOT = os.environ.get('PARSE_API_ROOT')

用pip進行安裝
sudo pip3 install git+https://github.com/xxxxxx/ParsePy.git


import os
os.environ["PARSE_API_ROOT"] = "https://your.domain.com/parse"
import datetime
from parse_rest.datatypes import Object
from parse_rest.connection import register

class Student(Object):
    pass



APPLICATION_ID = 'AppID'
REST_API_KEY = 'RestKey'
MASTER_KEY = 'MasterKey'

register(APPLICATION_ID, REST_API_KEY, master_key=MASTER_KEY)
s = Student.Query.get(objectId='xxxxaaaa10')
s.name = "C.Y.Fang"
s.math = 60
s.eng = 50
s.cht = 61
s.save()



參考資料:
https://github.com/milesrichardson/ParsePy

Raspberry Pi Startup run Python script



sudo chmod 755 xxx.py

sudo nano /etc/rc.local


#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi
sudo python xxx.py
exit 0

2018/08/04

Install BlueZ on the Raspberry Pi


sudo systemctl stop bluetooth
sudo apt-get update
sudo apt-get install libusb-dev libdbus-1-dev libglib2.0-dev libudev-dev libical-dev libreadline-dev libdbus-glib-1-dev unzip libcap2-bin
wget http://www.kernel.org/pub/linux/bluetooth/bluez-5.44.tar.xz
tar xf bluez-5.44.tar.xz
mv bluez-5.44 bluez
cd bluez
./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var --enable-library
make
sudo make install
sudo ln -svf /usr/libexec/bluetooth/bluetoothd /usr/sbin/
sudo install -v -dm755 /etc/bluetooth
sudo install -v -m644 src/main.conf /etc/bluetooth/main.conf
sudo systemctl daemon-reload
sudo systemctl start bluetooth
bluetoothd --version
sudo setcap 'cap_net_raw,cap_net_admin+eip' `which hcitool`
getcap !$

參考資料:
https://github.com/getsenic/gatt-python
Bluetooth LE scan as non root?

ImportError: No module named paho.mqtt.client


cd /usr/lib/python2.7/dist-packages
sudo ln -s /home/pi/.local/lib/python2.7/site-packages/paho


參考資料:
https://stackoverflow.com/questions/48752469/cannot-run-paho-mqtt-client-importerror-no-module-named-paho-mqtt-client