websocket with gunicorn

python 2017. 3. 3. 04:13

reference: http://d.hatena.ne.jp/Malan/20121007

flask + gevent websocket + gunicorn

실행 방법은

gunicorn --bind 0.0.0.0:yourwebsocketport -w 2 -k "geventwebsocket.gunicorn.workers.GeventWebSocketWorker" yourappfilename:app

으로 실행하면 아주 잘 되야한다.


그러나 쟁점은 gunicorn 실행시

if __name__ == "__main__" 에 걸리지 않는다는것이다.

걸리게 하려면 yourappfilename을 __main__대신 넣어주면 된다.


if __name__ == "yourappfilename"

그리고 실행하면 Address already in use를 만날수 있다.

그렇다면 살며시 이부분을 주석처리해준다.

# server = pywsgi.WSGIServer((myIP, config.NOTI_WS_PORT), app, handler_class=WebSocketHandler)
# server.serve_forever()


'python' 카테고리의 다른 글

flask with gevent monkey patch all  (0) 2017.07.26
flask gevent spawn use a lot of memory  (0) 2017.04.05
gunicorn vs uwsgi  (0) 2017.01.20
flask async response  (0) 2017.01.04
functools.wraps에 대해  (0) 2015.04.15
블로그 이미지

시간을 거스르는자

,


Reference: https://devcenter.heroku.com/articles/getting-started-with-go#deploy-the-app


0. Prepare your github project containing go app

this is mine: https://github.com/ytkang/golang_chat_bot

for newbee, important thing is your folder is going to be under src folder. so, there is no src or pkg folder in  github (related go structure).


if you already have $GOPATH just skip #1, 2 

1. Make some folder whatever you named, I named it "go"

$mkdir go
$cd go


2. Set gopath

go>$export GOROOT=/usr/local/go
go>$export GOPATH=`pwd`
go>$export PATH=$PATH:$GOPATH/bin


3. Login to heroku

you should install this first. 
https://devcenter.heroku.com/articles/getting-started-with-go#set-up

go>$heroku login


4. Get your project into this folder

go>$go get github.com/ytkang/golang_chat_bot

then, whatever there is an error or not,
"go/src/github.com/ytkang/golang_chat_bot" folder is created.

if you got an import like error when you "go get" then, It will be something occurred from your own module.

you should change your import path to solve this.

if you have folder and files like this,

github.com/ytkang/golang_chat_bot/chat.go
github.com/ytkang/golang_chat_bot/jarvis/jarvis.go

and if chat.go imports jarvis.go, then it should be like this

import github.com/ytkang/jarvis O
import ./jarvis X


4. Deploy to heroku!

go/src/github.com/ytkang/golang_chat_bot>$heroku create
go/src/github.com/ytkang/golang_chat_bot>$git push heroku master

if you got "reject" error something like "failed to detect set buildpack" then you should add "godep"


How to godep

$go get -u github.com/tools/godep   // install

go/src/github.com/ytkang/golang_chat_bot>$godep save  // make godep after this you got Godep folder

go/src/github.com/ytkang/golang_chat_bot>$git add -A; git commit -am "godep added" // important! you should apply this to your github

(after this, retry git push heroku master command. It should work!)


last command

go/src/github.com/ytkang/golang_chat_bot>$heroku open









'Go' 카테고리의 다른 글

Goroutine context switching 시점  (0) 2018.04.20
LITE IDE setup  (0) 2015.01.28
cannot download, $GOPATH not set  (0) 2015.01.28
블로그 이미지

시간을 거스르는자

,


std::move가 단지 캐스팅일 뿐이라니?

일단, move 구현을 보자

template<typename _Tp> inline typename std::remove_reference<_Tp>::type&& move(_Tp&& __t) { return static_cast<typename std::remove_reference<_Tp>::type&&>(__t); }

move가 무엇을 하는지 자세히 보니,

static_cast<typename std::remove_reference<_Tp>::type&&>(__t);

진짜 캐스팅뿐이다. 그렇다. source object가 없어지거나 그런거 아니다.

뭐 값을 대입시킬 객체에는 rvalue reference를 넘겨서 메모리주소를 그대로 전달한다고 치고,


이시점에서 궁금한건 오로지하나,

"뭐지? 그럼 기존 객체는 어떻게 없어지는거야?"

알고보니, 그 답은 rvalue reference 생성자와 대입연산자에 있었다.

rvalue reference 생성자와 대입연산자에서는 source object의 메모리 주소를 dest object에 주고

source object는 nullptr처리하거나 하는것이다.

대표적인 예가 std::string 이다.

assign(basic_string&& __str) { this->swap(__str); return *this; }

보면, source와 dest를 swap하고 있다. 따라서 string을 move시켜보면 move당한 놈은 "" 빈스트링을 갖게된다.

그럼 한번 테스트해볼까? 내가 만든 클레스를 move시킨면 어떻게되는지?

repl.it: https://repl.it/FbVT/0

code:

#include <iostream>
#include <stdlib.h>

class TestMe {

public:
TestMe(){ i = nullptr; }
void set(int& k){ i = &k; }
int get(){ return *i; }
void plus(){ (*i)++; }

private:
int* i;
};

int main()
{
        int test1 = 1;
        TestMe t1;
        t1.set(test1);

TestMe t2(std::move(t1));

t2.plus();
std::cout << "t1: " << t1.get() << std::endl;
std::cout << "t2: " << t2.get() << std::endl;

t1.plus();
std::cout << "t1: " << t1.get() << std::endl;
std::cout << "t2: " << t2.get() << std::endl;   

   return 0;

result:

 t1: 2
 t2: 2
 t1: 3
 t2: 3

아마도 이 결과로부터 유추할수있는건, 

std string처럼 swap같은건 없고 메모리는 일단 복사가되었는데 source object에 대한 후처리는 없다는 것이다. 

따라서 default move constructor가 어떻게 작동하는지 이해하고, 맘에 안든다면 직접 구현해야한다.



References

1) std::string source

2) std::remove_reference

3) What are rvalues, lvalues, xvalues, glvalues, and prvalues?


블로그 이미지

시간을 거스르는자

,