通常在寫 code 的時候都會使用 formater 去自動排版程式碼,在 Python 中我用了 autopep8 實現排版的動作,但個人覺得預設單行長度只有 80 太短了排出來很醜,那就來自己調整吧!
瀏覽分類:
心得筆記
[Python] Windows 下安裝 pyopenssl 時出現錯誤
緊接著 [Python] Windows 下安裝 pycrypto 時出現錯誤 又遇到個問題是在安裝 pyopenssl 的時候噴了下面的錯誤
C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Tools\MSVC\14.24.28314\bin\HostX86\x86\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -IC:\git\App\venv\include -IC:\Users\lex_xu\AppData\Local\Programs\Python\Python38-32\include -IC:\Users\lex_xu\AppData\Local\Programs\Python\Python38-32\include "-IC:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Tools\MSVC\14.24.28314\ATLMFC\include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Tools\MSVC\14.24.28314\include" "-IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\include\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\winrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\cppwinrt" /Tcbuild\temp.win32-3.8\Release\_openssl.c /Fobuild\temp.win32-3.8\Release\build\temp.win32-3.8\Release\_openssl.obj _openssl.c build\temp.win32-3.8\Release\_openssl.c(546): fatal error C1083: Cannot open include file: 'openssl/opensslv.h': No such file or directory error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Enterprise\\VC\\Tools\\MSVC\\14.24.28314\\bin\\HostX86\\x86\\cl.exe' failed with exit status 2 ---------------------------------------- Running setup.py clean for cryptography Failed to build cryptography Failed building wheel for cryptography Could not build wheels for cryptography which use PEP 517 and cannot be installed directly
[C#][TPL] 自定義 TaskScheuler 管理執行續
最近利用 Task 撰寫非同步的程式時碰到了許多未釐清的用法,這邊筆記一下自定義 TaskScheuler 的方法。
public class StaticThreadTaskScheduler : TaskScheduler, IDisposable { private readonly List<Thread> _threads = new List<Thread>(); private readonly BlockingCollection<Task> _tasks = new BlockingCollection<Task>(); private readonly AutoResetEvent _lockObj = new AutoResetEvent(true); public new int MaximumConcurrencyLevel { get; private set; } public StaticThreadTaskScheduler() : this(Environment.ProcessorCount) { } public StaticThreadTaskScheduler(int maximumConcurrencyLevel) { MaximumConcurrencyLevel = maximumConcurrencyLevel; initalize(); } protected override IEnumerable<Task> GetScheduledTasks() { return _tasks; } protected override void QueueTask(Task task) { if (task != null) { _lockObj.WaitOne(); _tasks.Add(task); _lockObj.Set(); } } protected override bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued) { return false; } private void initalize() { for (int i = 0; i < MaximumConcurrencyLevel; i++) { Thread thread = new Thread(execute); thread.IsBackground = true; thread.Start(); _threads.Add(thread); } } private void execute() { foreach (var task in _tasks.GetConsumingEnumerable()) { TryExecuteTask(task); } } #region IDisposable Support private bool _disposedValue = false; protected virtual void Dispose(bool disposing) { if (!_disposedValue) { if (disposing) { _lockObj.WaitOne(); foreach (var thread in _threads) { thread.Join(); } _threads.Clear(); _tasks.Dispose(); _lockObj.Dispose(); } _disposedValue = true; } } public void Dispose() { Dispose(true); } #endregion }
為了測試 StaticThreadTaskScheduler 是否真的如預期,我寫了一小段 Testing Code 去驗證。
class Program { static StaticThreadTaskScheduler _scheduler = new StaticThreadTaskScheduler(1); static void Main(string[] args) { foreach (var item in Enumerable.Range(1, 100)) { Task.Factory .StartNew(() => WorkingAsync(item), CancellationToken.None, TaskCreationOptions.None, _scheduler) .Unwrap(); } Console.ReadKey(); _scheduler.Dispose(); Console.ReadKey(); } static async Task WorkingAsync(int i) { while (true) { Console.WriteLine($"Thread ID: {Thread.CurrentThread.ManagedThreadId}, Working ID: {i}"); await Task.Factory .StartNew(async () => { await Task.Delay(1000); Console.WriteLine($"Thread ID: {Thread.CurrentThread.ManagedThreadId}, Sub Working ID: {i}"); }, CancellationToken.None, TaskCreationOptions.None, _scheduler) .Unwrap(); } } }
結果如下:
[C#][MSSQL] 連線結束後 Temp Table 沒有自動釋放 !?
上次 [C#] SQL 資料庫 Connection Pool 連線池觀念釐清 有提到可以開啟 Connection Pool 來減少開啟連線的效能耗損,但最近發現有 Stored Procedure 執行後沒有手動 Drop Temp Table,而 .NET 又將連線丟回 Pool 裡面造成 Temp Table 沒有被正常釋放的問題,下面來測試看看是不是真的會有這種情況發生。
[C#] Registrykey 登錄機碼的新增、修改、刪除、讀取
寫 Windows 應用程式時,常常會將一些參數存至機碼裡面,今天來簡單的筆記一下 Registry 的 CRUD。
在 64-bits 的作業系統下執行 32-bits 或 64-bits 應用程式時,預設寫入 Registry 會被作業系統寫入 Wow6432Node 的目錄底下。在 HKEY_LOCAL_MACHINE\SOFTWARE\
底下寫入機碼,就會出現在 HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\
,其目的是為了區分 32-bits 與 64-bits 作業系統的對應而產生的結果,只有真正的 64-bits 應用程式才能寫在 HKEY_LOCAL_MACHINE\SOFTWARE\
目錄。