2020年2月26日 星期三

Python Thread vs multiprocessing vs psutil

Python的Thread幾乎是廢了,可以改用multiprocessing和psutil取代。

Thread目前有一個大缺點,就是不會因為CPU是多核心,
就用多核執行,而是一次只執行一個Thread.
這點可用multiprocessing的Process取代,multiprocessing可用多核心,
不過有一個地方要注意,就是Process的全域變數,是使用script的initial value.
不像Thread仍然保留目前狀態的全域變數值,
當然,如果變數都沒有改變,那用initial value也沒有關係,
否則可將目前變數,當作arguments傳入。

另外,若是單純想對process做一簡單操作,可搭配使用psutil, e.g.
p = Process(target=example.main, args=())
p.start()
ps = psutil.Process(p.pid)

# 之後若是想
# 暫停
ps.suspend()

# 回復
ps.resume()

# 中止
ps.terminate()

# Thread的話,我查了半天,似乎沒有簡單做法可以達成
# Thread似乎是使用wait(), notify()... 但是不好弄,且好像無法簡單做成的樣子

2020年2月20日 星期四

AI Image Enlarger(人工智慧圖片無損放大工具)

AI Image Enlarger(人工智慧圖片無損放大工具)

2020年2月16日 星期日

QListWidget 一個項目,卻顯示一行名稱,另一行整個空白

查看是否另一行空白,是由換行字元所造成?
解法有二,擇一可解:
1. 將換行字元刪除

2. Checked uniformItemSizes

2020年2月10日 星期一

Show All Running Processes in Linux using ps/htop commands

Show All Running Processes in Linux using ps/htop commands


How to list process with the ps command

Type the following ps command to display all running process:
# ps -aux | less
OR
# ps aux | less
Where,
  • A : Select all processes
  • u : Select all processes on a terminal, including those of other users
  • x : Select processes without controlling ttys

2020年2月9日 星期日

running a command on pi boot after IP is assigned

running a command on pi boot after IP is assigned

在Raspberry Pi 4B 上,試過有效。

You can make a systemd service for your program and enable it via systemctl.
First, create a file /etc/systemd/system/<name>.service on the Pi. (Be sure to replace <name> with a actual service name.)
sudo touch /etc/systemd/system/<name>.service
Write in this file with your favorite text editor (nano, etc):
[Unit]
Description=blah service
After=network.target

[Service]
Type=oneshot
User=pi
ExecStart=yourcommand


[Install]
WantedBy=multi-user.target
Replace yourcommand with the command you want to run. Then save the file and close the text editor.
Next, reload the daemon to make systemd recognize your service:
sudo systemctl daemon-reload
Then, we can make it run on boot now:
sudo systemctl enable <name>
(Again, make sure to replace <name> with the name of your service.)
Enjoy your start-on-boot command!
EDIT: didn't realize this post was that old actually. but anyways, either mark a answer as correct or state your solution in a answer and mark that so it doesn't show up on the front page

2020年2月5日 星期三

Reading and Writing JSON to a File in Python

Reading and Writing JSON to a File in Python


Writing JSON to a File

The easiest way to write your data in the JSON format to a file using Python is to use store your data in a dict object, which can contain other nested dicts, arrays, booleans, or other primitive types like integers and strings. You can find a more detailed list of data types supported here.
The built-in json package has the magic code that transforms your Python dict object in to the serialized JSON string.
import json

data = {}
data['people'] = []
data['people'].append({
    'name': 'Scott',
    'website': 'stackabuse.com',
    'from': 'Nebraska'
})
data['people'].append({
    'name': 'Larry',
    'website': 'google.com',
    'from': 'Michigan'
})
data['people'].append({
    'name': 'Tim',
    'website': 'apple.com',
    'from': 'Alabama'
})

with open('data.txt', 'w') as outfile:
    json.dump(data, outfile)

Reading JSON from a File

On the other end, reading JSON data from a file is just as easy as writing it to a file. Using the same json package again, we can extract and parse the JSON string directly from a file object. In the following example, we do just that and then print out the data we got:
import json

with open('data.txt') as json_file:
    data = json.load(json_file)
    for p in data['people']:
        print('Name: ' + p['name'])
        print('Website: ' + p['website'])
        print('From: ' + p['from'])
        print('')



2020年2月3日 星期一

[轉載]Linux中tty、pty、pts的概念區別

[轉載]Linux中tty、pty、pts的概念區別

轉自 http://www.wretch.cc/blog/redsonoma/14021073

1> tty(終端設備的統稱):
tty一詞源於Teletypes,或者teletypewriters,原來指的是電傳打字機,是通過串行線用打印機鍵盤通過閱讀和發送信息的東西,後來這東西被鍵盤與顯示器取代,所以現在叫終端比較合適。
終端是一種字符型設備,它有多種類型,通常使用tty來簡稱各種類型的終端設備。


2> pty(虛擬終端):
但是如果我們遠程telnet到主機或使用xterm時不也需要一個終端交互麼?是的,這就是虛擬終端pty(pseudo-tty)


3> pts/ptmx(pts/ptmx結合使用,進而實現pty):
pts(pseudo-terminal slave)是pty的實現方法,與ptmx(pseudo-terminal master)配合使用實現pty。

2020年2月2日 星期日

Ls Command in Linux (List Files and Directories)

Ls Command in Linux (List Files and Directories)


ls -l                         use a long listing format

Long Listing Format

The default output of the ls command shows only the names of the files and directories, which is not very informative.
The -l ( lowercase L) option causes ls to print files in a long listing format.
When the long listing format is used, the ls command will display the following file information:
  • The file type
  • The file permissions
  • Number of hard links to the file
  • File owner
  • File group
  • File size
  • Date and Time
  • File name
Consider the following example:
ls -l /etc/hosts
-rw-r--r-- 1 root root 337 Oct  4 11:31 /etc/hosts

Let’s explain the most important columns of the output.
The first character shows the file type. In our example, the first character is - which indicates a regular file. Values for other file types are as follows:
  • - - Regular file
  • b - Block special file
  • c - Character special file
  • d - Directory
  • l - Symbolic link
  • n - Network file
  • p - FIFO
  • s - Socket
The next nine characters are showing the file permissions. The first three characters are for the user, the next three are for the group, and the last three are for others. You can change the file permissions with the chmod command. The permission character can take the following value:
  • r - Permission to read the file
  • w - Permission to write to the file
  • x - Permission to execute the file
  • s - setgid bit
  • t - sticky bit
In our example, rw-r--r-- means that the user can read and write the file, and the group and others can only read the file. The number 1 after the permission characters is the number of hard links to this file.
The next two fields root root are showing the file owner and the group, followed by the size of the file (337), shown in bytes. Use the -h option if you want to print sizes in a human-readable format. You can change the file owner using the chown command.
Oct 4 11:31 is the last file modification date and time.
The last column is the name of the file.

Show Hidden Files

By default, the ls command will not show hidden files. In Linux, a hidden file is any file that begins with a dot (.).
To display all files including the hidden files use the -a option:
ls -la ~/
drwxr-x--- 10 linuxize  linuxize  4096 Feb 12 16:28 .
drwxr-xr-x 18 linuxize  linuxize  4096 Dec 26 09:21 ..
-rw-------  1 linuxize  linuxize  1630 Nov 18  2017 .bash_history
drwxr-xr-x  2 linuxize  linuxize  4096 Jul 20  2018  bin
drwxr-xr-x  2 linuxize  linuxize  4096 Jul 20  2018  Desktop
drwxr-xr-x  4 linuxize  linuxize  4096 Dec 12  2017 .npm
drwx------  2 linuxize  linuxize  4096 Mar  4  2018 .ssh